0

I have an index page where there is a list of users , if user clicks on each user it goes to their profile screen , I can pass the id to render the profile screen but the problem is the profile page is made of several components , and I need that received data to be used in components.

Here is how I pass the data to user's profile:

onPress={() => this.props.navigation.navigate('userProfile',{item})}>

Here is how I receive it in userProfile :

    export default class UserProfileScreen extends React.Component {

      render() {
        const {item} = this.props.navigation.state.params;
        return (
          <ScrollView>
            <View style={styles.container}>
              <IntroCard item={item}/>
            </View>
          </ScrollView>
}
}

and this is how I receive data in <IntroCard> component:

const IntroCard = (props) => {
  return (
    <View>
       <Text>{this.props.item ? this.props.item.id : 'default'}</Text>
    </View>
)}

But after doing all of these I get the following error:

TypeError: TypeError: TypeError: undefined is not an object (evaluating '_this.props.item')
hesam sameni
  • 131
  • 3
  • 16
  • Possible duplicate of [Pass Data between Pages in React native](https://stackoverflow.com/questions/50098376/pass-data-between-pages-in-react-native) – Andrew Mar 12 '19 at 08:34

1 Answers1

0

You can pass it as a prop to the components

render(){
  const {item} = this.props.navigation.state.params;
  return (
    <View>
      <Component item={item}/>
    </View>
  )
}

then access it with this.props on Component

<Text>{props.item ? props.item.id : 'print something else'}</Text>
hesam sameni
  • 131
  • 3
  • 16
wicky
  • 948
  • 6
  • 13