【问题标题】:How to pass values to other component in React-Native-Router-Flux?如何将值传递给 React-Native-Router-Flux 中的其他组件?
【发布时间】:2016-09-21 08:46:03
【问题描述】:

我的代码是:

...
<Router>
 <Scene key="com1" component={Com1} initial/>
<Scene key="com2" component={Com2}/>
</Router>
...
com1.js
...
onPress={Actions.com2}

我将com1 更改为com2

但我需要将Com1 的输入框的值传递给Com2

我该怎么做?

【问题讨论】:

    标签: react-native react-native-router-flux


    【解决方案1】:

    你可以这样传递数据:

    Actions.com2 ({text: 'Hello World'})

    您可以像这样在 com2 中恢复数据:

    this.props.text

    你可以去下一个教程了解更多信息:

    https://github.com/aksonov/react-native-router-flux/blob/master/docs/v3/MINI_TUTORIAL.md

    【讨论】:

    • 非常感谢
    • @achu 你也在使用 Redux 吗?
    【解决方案2】:

    除此之外,(对于那些说它不起作用的 cmets 中的人)您可能想在下面尝试。当你通过

    Actions.com2({text : 'Hello World'});
    

    Com2 应该通过 'props'

    const Com2 = (props) => {
       return ( <View ...
        {props.text}
       ... />
    );
    

    【讨论】:

    • 如果我使用基于类的组件怎么办?
    【解决方案3】:

    通过 Input 传递数据,

       import React, { Component } from 'react';
       import { Text, View, TextInput, TouchableOpacity } from 'react-native';
       import { Actions } from 'react-native-router-flux';
    
       export default class Com1 extends Component {
       state = { text: '' };
          render() {
           return (
              <View>
                 <TextInput
                    value={this.state.text}
                    onChangeText={text => this.setState({ text })}
                />
        <TouchableOpacity onPress={this.onPressNext.bind(this)}>
       <Text>Get Data</Text>
        </TouchableOpacity>
        </View>
        );
    }
    
    onPressNext() {
        Actions.Com2({text: this.state.text });
    }
    }
    

    在第二页获取价值

       export default class Com2 extends Component {
    
          render() {
             return (
            <View>
             <Text>
              {this.props.text}
             </Text>
           </View>
        );
      }
     }
    

    你可以参考这个链接: https://react-native-solutions.blogspot.com/2018/07/passing-data-between-screens-in-react.html

    【讨论】:

      【解决方案4】:

      使用推送方式如下:

      Actions.push('your key name',{prop you want to pass})
      

      它将场景推送到堆栈,执行到新场景的过渡。

      【讨论】:

      【解决方案5】:

      你可以使用带有道具的动作

      onPress={() => Actions.com2({title: 'some title'})}
      

      然后在 com2 中你可以像这样检索数据

      this.props.title
      

      或者你可以使用推送属性

       Actions.push('com2',{title: 'some title' } );
      

      然后在 com2 中也这样检索它

      this.props.title
      

      【讨论】:

        猜你喜欢
        • 2018-08-25
        • 2017-08-25
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-12-30
        • 1970-01-01
        相关资源
        最近更新 更多