如何使用React Native中的导航器动态地向组件添加新道具

vas*_*avi 5 javascript jquery reactjs react-native

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

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

this.props.navigator.push({
     id: 'DisplayCustomSchema',
     name: 'DisplayCustomSchema',
     org:this.props.org
});
Run Code Online (Sandbox Code Playgroud)

在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>
      }
})
Run Code Online (Sandbox Code Playgroud)

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

Dan*_*idt 1

你有两个选择

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

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

    2. 如果您使用 redux,您可以在安装时分派一个操作,然后更改存储以包含加载的道具。

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