Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.6k views
in Technique[技术] by (71.8m points)

android - How to pass data between react native screens?

Hey I am new to react native. So basically I have a stack navigator with two screen option: Screen 1 (Default) and Screen2. I already set up a button so that when pressed it will take me to Screen 2. So in Screen1 I have an array displayed as scrollview component. Now when i press the button to go to screen2 I want to pass some of the array values to that screen2 component. What is the best way to do it? Thank you :)

I am really new so my attempts are kinda dumb. I tried importing the Screen1 component and calling the array values via this.state but no access.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

The easiest way to pass the data is when you are navigating. To do that it is better that you put your array in a state an then use the following method:

onPress=()=>{
     this.props.navigation.navigate('Screen2', {
          yourArray: this.state.yourArray, });
      }
}

Then in the next screen(Screen2) you can find the array(your data) in the props.So in your constructor in scrren2 you can find the data here:

  constructor(props) {
    super(props);

    var params = props.navigation.state.params.yourArray;

}

Also to come back to your previous screen without touching your states you can use this code in your back button:

  const { goBack } = this.props.navigation;
      goBack();

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...