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
527 views
in Technique[技术] by (71.8m points)

navigation - How can I pass a const to an other screen in react native?

I have an filtered Array "newAsanasAnfaenger" which I want to pass to another screen to use it for a slide show. How can I pass it to for exampe another class called "UebungenAnzeige"? How can I pass a whole array? Or is there a way to export two things from one file (e.g. class and const)?

render() {
   const {category} = this.props.route.params;
   const { navigation } = this.props;
   const newAsanasAnfaenger = asanas.filter(asana => asana.kategorie == category);
 
   return (
     <View>
      <View style={stylesUebungen.container2}>
       <FlatList
         data={newAsanasAnfanger}
         renderItem={this.renderEntry}
         keyExtractor={item => `${item.id}`}
       />
       
        <Button title="Go back" onPress={() => this.props.navigation.navigate('Auswahl')} />
       </View>
     </View>
   );
question from:https://stackoverflow.com/questions/66051022/how-can-i-pass-a-const-to-an-other-screen-in-react-native

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

1 Answer

0 votes
by (71.8m points)
this.props.navigation.navigate('Auswahl', { myProp: newAsanasAnfaenger } )}

Then in your other component:

const { myProp } = this.props.route.params;

You can find more information about passing parameters to routes in the React Navigation documentation: https://reactnavigation.org/docs/params/

Your second question seems unrelated to the first, but the answer is yes, you can export multiple things from one file. For example:

export const myVar = 1;
export function myFunc() { return true; }

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

...