代码之家  ›  专栏  ›  技术社区  ›  Somename

如何在StackNavigator中将参数传递到屏幕?

  •  63
  • Somename  · 技术社区  · 7 年前

    我的本地代码:

    import React, { Component } from 'react';
    import { AppRegistry, ActivityIndicator, StyleSheet, ListView, 
      Text, Button, TouchableHighlight, View } from 'react-native';
    
    import { StackNavigator } from 'react-navigation';
    import DetailsPage from './src/screens/DetailsPage';
    
    class HomeScreen extends React.Component {
    
       constructor() {
        super();
        const ds = new ListView.DataSource({rowHasChanged: (r1, r2) => r1 !== r2});
        this.state = {
          userDataSource: ds,
        };
      }
    
      componentDidMount(){
          this.fetchUsers();
      }
    
        fetchUsers(){
    
            fetch('https://jsonplaceholder.typicode.com/users')
                .then((response) => response.json())
                .then((response) => {
                    this.setState({
                        userDataSource: this.state.userDataSource.cloneWithRows(response)
                    });
                });
        }
    
        onPress(user){
            this.props.navigator.push({
                id: 'DetailPage'
            });
        }
    
      renderRow(user, sectionID, rowID, highlightRow){
          return(
          <TouchableHighlight onPress={()=>{this.onPress(user)} } >
          <View style={styles.row}>
            <Text style={styles.rowText}> {user.name} </Text>
    
          </View>
          </TouchableHighlight>
          )
      }
    
    
      render(){
          return(
              <ListView
                dataSource = {this.state.userDataSource}
                renderRow = {this.renderRow.bind(this)}
              />
          )
      } 
    }
    

    const NavigationTest = StackNavigator({
      Home: { screen: HomeScreen },
      DetailsPage: { screen: DetailsPage },
    });
    

    详细信息屏幕为:

    import React, { Component } from 'react';
    import { StyleSheet, Text, View } from 'react-native';
    
    import styles from '../styles';
    
    
    export default class DetailsPage extends React.Component {
      static navigationOptions = ({ navigation }) => ({
        title: `User: ${navigation.state.params.user.name}`,
      });
      render() {
        const { params } = this.props.navigation.state;
        return (
          <View>
            <Text style={styles.myStyle}>Name: {params.name}</Text>
            <Text style={styles.myStyle}>Email: {params.email}</Text>
          </View>
        );
      }
    }
    

    我不能通过考试 user DetailsPage 代码为:

    onPress(user){
            this.props.navigator.push({
                id: 'DetailPage'
            });
        }
    

    我想导航到 DetailPage onPress 作用如果我像这样提醒它:

    onPress(user){ Alert.alert(user.name)}
    

    非常感谢!

    8 回复  |  直到 7 年前
        1
  •  125
  •   jjjjjjjjjjjjjjjjjjjj    2 年前

    您可以使用 navigate

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

    反应导航5.x,6.x(2022)

    在中访问它们 this.props.route.params 。例如 DetailsPage

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

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

    反应导航<=4.x

    在中访问它们 this.props.navigation.state.params 详细信息页面

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

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

        2
  •  9
  •   Aurangzaib Rana    5 年前

    在react钩子中,参数使用useNavigation发送导航

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

    function MyScreen() {
      const name = useNavigationParam('name');
      return <p>name is {name}</p>;
    }
    
        3
  •  6
  •   4b0 Agit    4 年前

    See this .

    它解决了我的问题。

    this.props.navigation.navigate("**stack_Name**", {
     screen:"screen_name_connect_with_**stack_name**",
     params:{
     user:"anything_string_or_object"
    }
    })
    
        4
  •  2
  •   Dharman Aman Gojariya    4 年前

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

    对于第二个页面名,请说ListItemPageRoute

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

    '道具。“路由”对象将包含您希望从一个屏幕传递到另一个屏幕的所有参数的列表。

        5
  •  0
  •   rubal islam    4 年前

    对于功能部件:

    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();
          }}
        />
      );
    }
    

    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} />;
    }
    
        6
  •  0
  •   Hamza Ishfaq    4 年前

    onPress={()=>{this.props.navigation.navigate('AllImages',{Types:item.type}}) 控制台.log('值 强文本 ,this.props.route.params.Types)**

        7
  •  0
  •   Muhammad Haidar    4 年前

    onPress={(item)=>props.navigation.navigate('你的屏幕名',{item:item})}

    2) 如何在另一个屏幕上获取数据: 常量{item}=props.route.params

        8
  •  -1
  •   KAYZORK    3 年前


    export default () => {
      
      const MainScreenComponent = ({navigation}) => (
        <MainScreen
          navigation={navigation}
          /**
          *
          * And here your parameters...
          *
          */
        />
      );
    
      const HomeScreenComponent = ({navigation}) => (
        <HomeScreen
          navigation={navigation}
          /**
          *
          * And here your parameters...
          *
          */
        />
      );
    
      return (
        <NavigationContainer>
          <Stack.Navigator initialRouteName="MainScreen">
            <Stack.Screen name="MainScreen" component={MainScreenComponent}/>
            <Stack.Screen name="HomeScreen" component={MomeScreenComponent}/>
          </Stack.Navigator>
        </NavigationContainer>
      );
    };