How to Pass Parameters to screen in StackNavigator?

后端 未结 6 1356
抹茶落季
抹茶落季 2020-12-13 05:46

My React Native code:

import React, { Component } from \'react\';
import { AppRegistry, ActivityIndicator, StyleSheet, ListView, 
  Text, Button, TouchableH         


        
相关标签:
6条回答
  • 2020-12-13 06:26

    In your first page, say CountriesList

    const CountriesList = ({ navigation }) => {
    
    /* Function to navigate to 2nd screen */
      const viewCountry = (country) => {
        navigation.navigate('ListItemPageRoute', { name: country.country_name });
      };
    }
    

    For your second page name, say ListItemPageRoute

    const ListItemPageRoute = (props) => {
    
    return (
        <View style={styles.container}>
            <Text style={styles.countryText}> { props.route.params.name } </Text>
        </View>
      );
    };
    

    'Props.route' Object will have the list of all parameters you would like to pass from one screen to another.

    0 讨论(0)
  • 2020-12-13 06:33

    In react hooks, params send in navigation using useNavigation

    import { useNavigation } from '@react-navigation/native';
    
    const navigation = useNavigation();
    
    <Button
          title="Back"
          onPress={() => {
            navigation.navigate('MyScreen',{name:'test'});
          }}
        />
    

    then access them using useNavigationParam

    function MyScreen() {
      const name = useNavigationParam('name');
      return <p>name is {name}</p>;
    }
    
    0 讨论(0)
  • 2020-12-13 06:44

    onPress={() => {this.props.navigation.navigate('AllImages', {Types: item.type })} console.log('valuesstrong text', this.props.route.params.Types);**

    0 讨论(0)
  • 2020-12-13 06:45

    See this .

    It solved my problem.

    this.props.navigation.navigate("**stack_Name**", {
     screen:"screen_name_connect_with_**stack_name**",
     params:{
     user:"anything_string_or_object"
    }
    })
    
    0 讨论(0)
  • 2020-12-13 06:50

    for me, soved it by useNavigation() and useRoute():

    For functional component:

    import * as React from 'react';
    import { Button } from 'react-native';
    import { useNavigation } from '@react-navigation/native';
    
    function MyBackButton() {
      const navigation = useNavigation();
    
      return (
        <Button
          title="Back"
          onPress={() => {
            navigation.goBack();
          }}
        />
      );
    }
    

    For class component:

    class MyText extends React.Component {
      render() {
        // Get it from props
        const { route } = this.props;
      }
    }
    
    // Wrap and export
    export default function(props) {
      const route = useRoute();
    
      return <MyText {...props} route={route} />;
    }
    
    0 讨论(0)
  • 2020-12-13 06:51

    You can pass params with the navigate function's second argument:

    onPress(user) {
      this.props.navigation.navigate(
        'DetailPage',
        { user },
      );
    }
    

    React Navigation 5.x (2020)

    Access them in this.props.route.params. For example in your DetailsPage:

    <Text style={styles.myStyle}>{this.props.route.params.user.name}</Text>
    

    https://reactnavigation.org/docs/params/

    React Navigation <= 4.x

    Access them in this.props.navigation.state.params. For example in your DetailsPage:

    <Text style={styles.myStyle}>{this.props.navigation.state.params.user.name}</Text>
    

    https://reactnavigation.org/docs/4.x/params/

    0 讨论(0)
提交回复
热议问题