如何在 StackNavigator 中将参数传递给屏幕? [英] How to Pass Parameters to screen in StackNavigator?

查看:36
本文介绍了如何在 StackNavigator 中将参数传递给屏幕?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的 React Native 代码:

My React Native code:

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:

I am not able to pass the user to the DetailsPage with the code:

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

我想使用 onPress 函数导航到 DetailPage.如果我提醒它:

I want to navigate to the DetailPage with the onPress function. If I alert it like:

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

我确实得到了值,但如何将其传递给其他页面?

I do get the value, but how do I pass it to the other page?

非常感谢!

推荐答案

您可以使用 navigate 函数的第二个参数传递参数:

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

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

React 导航 5.x (2020)

this.props.route.params 中访问它们.例如在您的 DetailsPage 中:

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/

this.props.navigation.state.params 中访问它们.例如在您的 DetailsPage 中:

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/

这篇关于如何在 StackNavigator 中将参数传递给屏幕?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆