如何在导航到上一个屏幕时传递参数?

How to pass parameter while navigating to previous screen?

我正在从屏幕 A 导航到屏幕 B.. 然后使用

导航回屏幕 A
this.props.navigation.goBack(null);

我想知道如何在执行此操作时传递参数?

这是我的代码

 import React, { Component } from 'react';
 import { View, Text, TextInput, ScrollView } from 'react-native';
 import Card from './Card';
 import CardSection from './CardSection';
 import MyButton from './MyButton';


 class Settings extends Component{
 constructor(props){
 super(props);
 this.state = {
   ip:'',
   port:''
 };
}

onSaveClick() {
 console.log('Button clicked');
 this.props.navigation.goBack();
}
render(){
const { input, container } = styles;
return(
  <View style = {container}>
  <Card>
  <CardSection>
    <TextInput 
      style = {input}
      onChangeText = {(ip) => this.setState({ip})}
      placeholder = "IP Address"
      autoCapitalize = "none"
      autoCorrect = {false}
      keyboardType = "numeric"
    />
    </CardSection>
    <CardSection>
      <TextInput
      style={styles.input}
      onChangeText={(port) => this.setState({port})}
      placeholder="Port Number"
      autoCapitalize="none"
      autoCorrect={false}
      keyboardType = "numeric"
    />
    </CardSection>
    <CardSection>
      <MyButton onPress ={this.onSaveClick.bind(this)}>
        Save
      </MyButton>
    </CardSection>
    <View>
    </View>
   </Card> 
  </View>
);
}
}

那么我怎样才能在上一个组件中访问这个组件的状态。我们可以将状态作为参数传递吗?

You should follow the Redux methodology

因为将状态/信息从子组件传回父组件(第二个屏幕到第一个屏幕)是一个糟糕的设计模式,因为这会在应用程序中创建多个数据流路径,这使得调试变得困难。

redux 的作用是

  1. 当点击提交时,你将数据推送到一个全局存储中。(基本上调度一个动作并使用 reducer 来更新全局 redux 存储)

  2. 返回上一屏幕。

  3. 通过连接(来自 react -redux 包)列出上一页中的这些更改并反映这些更改。

您可以使用节点包 react-redux 。

并从这里继续这个例子 -> http://redux.js.org/docs/basics/ExampleTodoList.html

更多有用的示例 here .