Make a reusable component in react native?

夙愿已清 提交于 2020-01-05 04:31:13

问题


I make a reusable component to render a Flatlist and inside I have a TouchableOpacity when pressed it I want to navigate to a specific screen, and I pass some params based on items inside FlateList

So when I pressed to navigate i got an error

can't read property navigate of undefined

here is my code

const OrdersList = props => {
  return (
    <View style={styles.container}>
      <FlatList
        data={props.data}
        renderItem={({item}) => {
          return (
            <TouchableOpacity
              onPress={() =>
                props.navigation.navigate(props.screenName, {
                  ...
                  service: item.service,
                  date: item.date,
                  ...
                })
              }
             >
                <Text>
                  {item.service}
                </Text>
            </TouchableOpacity>
          );
        }}
      />
    </View>
  );
};

here is how I used it

 <OrdersList
          data={this.state.orders}
          extraData={this.state}
          screenName="OrderHomeDetails"
          // navigate={ // when i do it i got error he can't find item and thats usual 
          //   (this.props.navigation.navigate,
          //   {
          //     service: item.service,
          //     time: item.time,
          //     username: item.username,
          //     buildingNumber: item.buildingNumber,
          //     date: item.date,
          //     description: item.description,
          //     images: item.images,
          //     status: item.status,
          //     snapshotKey: item.snapshotKey,
          //   })
          // }
        />

回答1:


You error is telling me that whatever component is rendering OrdersList is not receiving navigation props. If this component is a screen, it should be navigated to in order to receive the navigation prop (which means it is listed on whatever create*navigator you used).

As I have not full access to your code, I can suggest you to use withNavigation decorator on OrdersList, so you no longer have to pass navigation props from the parent.

const OrdersList_ = props => {
  return (
    <View style={styles.container}>
      <FlatList
        data={props.data}
        renderItem={({item}) => {
          return (
            <TouchableOpacity
              onPress={() =>
                props.navigation.navigate(props.screenName, {
                  ...
                  service: item.service,
                  date: item.date,
                  ...
                })
              }
             >
                <Text>
                  {item.service}
                </Text>
            </TouchableOpacity>
          );
        }}
      />
    </View>
  );
};

const OrdersList = withNavigation(OrdersList)

 <OrdersList
          data={this.state.orders}
          extraData={this.state}
          screenName="OrderHomeDetails"
          // navigate={ // when i do it i got error he can't find item and thats usual 
          //   (//no need to pass navigation props anymore
          //   {
          //     service: item.service,
          //     time: item.time,
          //     username: item.username,
          //     buildingNumber: item.buildingNumber,
          //     date: item.date,
          //     description: item.description,
          //     images: item.images,
          //     status: item.status,
          //     snapshotKey: item.snapshotKey,
          //   })
          // }
        />

Reference: https://reactnavigation.org/docs/en/with-navigation.html




回答2:


const OrdersList = props => {
    return (
      <View >
        <FlatList
          data={props.data}
          renderItem={({item}) => {
            return (
              <TouchableOpacity
                onPress={()=>{

                props.navigate(props.screen,{item:item, item2:props.additionalProp})
                   }
                }
               >
                  <Text>
                    {item.service}
                  </Text>
              </TouchableOpacity>
            )}}
        />
      </View>
    );
  };
  export default class Prova extends Component{

render(){
    return(
        <View>
        <OrdersList
        data={[{service:'service',data:'2019-12-12'},{}]}
        navigate={this.props.navigation.navigate}
        screen={'YourScreen'}
        additionalProp={"additional prop"}
        ></OrdersList>
        </View>
    )}

  }


来源:https://stackoverflow.com/questions/59292409/make-a-reusable-component-in-react-native

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!