1

I am new to react native how can i send data from one screen to another screen using props in android not for ios my code is as below

Home.js

class Home extends React.Component {

  constructor(props) {
    super(props);
    this.state = {
      qwerty:{
        data:[],
      },
    };
  }

  goPressed(navigate){
     navigate("Product");
  }

  render() {
      const { navigate } = this.props.navigation;

      contents = this.state.qwerty.data.map((item) => {
        return (
            <View key={item.p1.id}>
            <View>
              <Text>{item.p1.content}</Text>
            </View>
            <View>

            <TouchableHighlight onPress={() => this.goPressed(navigate)}>
              <Text>
                Go
              </Text>
            </TouchableHighlight>

            </View>
          </View>
          );
       });

      return (
        <ScrollView style={styles.container}>
          {contents}
        </ScrollView>
      );
    }
  }

  export default Home;

this is my home.js , I want pass data i.e {item.p1.content} to another screen i.e product.js so how can i do it what modification should i do?

Product.js

      export default class Products extends Component {
  static navigationOptions = {
    title: "Products",
  };

  render() {
    return (
      <View style={{ flex: 1 }}>
        <Text>{item.p1.content}</Text>
      </View>
    );
  }
}

2 Answers 2

1

Send data to other screen

this.props.navigation.navigate('Your Screen Name' , { YourParamsName: "Foo"});

Receive data from other screen

this.props.navigation.state.params.YourParamsName
Sign up to request clarification or add additional context in comments.

Comments

0

One method is to simply pass the date you are storing in 'qwerty' as a props to the next scene.

In Home.js you can modify your goPressed method to be something like...

  goPressed(navigate){
     navigate("Product", {passedData: this.state.qwerty.item.p1.content});
  }

Then in Product.js you will need to modify the code to

render() {
    return (
      <View style={{ flex: 1 }}>
        <Text>{this.props.passedData}</Text>
      </View>
    );
  }

2 Comments

hello.. using this method it is navigating but data is not passed to Product & showing undefined when cheked on console. Is any need to import something or set state in Product.js.
what do you get when you change Home.js to: navigate("Product", {passedData: this.state.qwerty});

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.