【问题标题】:How to add new props dynamically to the component using navigator in React Native如何在 React Native 中使用导航器向组件动态添加新道具
【发布时间】:2015-11-05 04:44:46
【问题描述】:

在我的场景中,我使用导航器渲染组件。在该组件中,我执行了一些操作,例如保存记录,然后我向现有组件动态添加了一个新道具。但是这里的动态道具在该组件中不可见,只有以前的道具才可见。如何使用navigator实现给组件添加动态prop。

这里我在使用 React Native 导航器时提供代码我正在调用 DisplayCustomSchema 组件,如下所示

this.props.navigator.push({
     id: 'DisplayCustomSchema',
     name: 'DisplayCustomSchema',
     org:this.props.org
});

在 DisplayCustomSchema 组件 componentDidMount 中,我调用了一个 ajax 发布它返回一些数据。

var DisplayCustomSchema = React.createClass({

         componentDidMount : function(){
             ajaxpost(data){//example only
               this.props.record=data
              //here i am try to move response data to props because I am using this data in saveRecord function but in save record props contain name and org only


       }
      }
      render: function(){
                <View>
                  <TouchableHighlight style={styles.touchButtonBorder} underlayColor="#ffa456" onPress={saveRecord.bind(this,this.props)}>
                    <Text style={styles.button}>SAVE</Text>
                  </TouchableHighlight>
                </View>
      }
})

我的问题是为什么数据不移动到道具

【问题讨论】:

    标签: javascript jquery reactjs react-native


    【解决方案1】:

    你有两个选择

    1. 使用state
    2. 使用像redux这样的框架

      1. 由于无法设置 props,可以使用 this.setState 设置加载的数据

      2. 如果你使用 redux,你可以在 mount 时调度一个 action,然后改变 store 以包含加载的 props。

    请注意,强烈建议使用第二个选项,因为使用状态是一种反模式。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-09-22
      • 1970-01-01
      • 2019-07-22
      • 1970-01-01
      • 2020-05-23
      • 1970-01-01
      相关资源
      最近更新 更多