[英]How to add new props dynamically to the component using navigator in React Native
In my scenario I am rendering the component using navigator. 在我的场景中,我使用导航器渲染组件。 In that component I perform some action like save the record then I added a new prop dynamically to the existing component.
在该组件中,我执行了一些操作,例如保存记录,然后我动态地向现有组件添加了一个新的prop。 But here the dynamic prop is not visible in that component only previous props only visible.
但是这里的动态道具在该组件中是不可见的,只有之前的道具才可见。 How to achieve adding dynamic prop to the component using navigator.
如何使用导航器为组件添加动态道具。
here I am providing code while using React Native navigator I am calling DisplayCustomSchema component like as below 这里我在使用React Native导航器时提供代码我正在调用DisplayCustomSchema组件,如下所示
this.props.navigator.push({
id: 'DisplayCustomSchema',
name: 'DisplayCustomSchema',
org:this.props.org
});
In DisplayCustomSchema component componentDidMount
I am calling one ajax post it it return some data. 在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>
}
})
here my question is why data not moves to props 这里我的问题是为什么数据不会移动到道具
You have two options 你有两个选择
Use a framework like redux 使用像redux这样的框架
As you can't set props you may use this.setState to set the loaded data 由于无法设置道具,因此可以使用this.setState设置加载的数据
If you use redux, you may dispatch an action on mounting, which then changes the store to include the loaded props. 如果您使用redux,您可以在安装时调度操作,然后更改商店以包含已加载的道具。
Note that the second option is strongly encouraged, as using the state is an antipattern. 请注意,强烈建议使用第二个选项,因为使用state是反模式。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.