在我的场景中,我使用导航器渲染组件。在该组件中,我执行了一些操作,例如保存记录,然后我向现有组件动态添加了一个新 Prop 。但是这里动态 Prop 在该组件中不可见,只有以前的 Prop 才可见。如何使用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>
}
})
我的问题是为什么数据没有转移到 props 上
最佳答案
你有两个选择
- 使用state
使用类似 redux 的框架
因为你不能设置 props 你可以使用 this.setState 来设置加载的数据
如果您使用 redux,您可以在安装时分派(dispatch)一个操作,然后更改商店以包含加载的 Prop 。
请注意,强烈建议使用第二个选项,因为使用状态是一种反模式。
关于javascript - 如何在 React Native 中使用导航器向组件动态添加新 Prop ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33536870/