简体   繁体   English

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

[英]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 你有两个选择

  1. Use the state 使用状态
  2. Use a framework like redux 使用像redux这样的框架

    1. As you can't set props you may use this.setState to set the loaded data 由于无法设置道具,因此可以使用this.setState设置加载的数据

    2. 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.

相关问题 react-native:无法向组件添加道具 - react-native : cant add props to component 如何使用 props 为 React 组件动态构建 object? - How to dynamically build an object for a React Component using props? React-Native传递道具而不使用导航器? - React-Native passing props without using navigator? React Native:如何将道具传递给&#39;Navigator.NavigationBar&#39;的&#39;routeMapper&#39;? - React Native: How to pass props to 'routeMapper' of 'Navigator.NavigationBar'? 如何将道具传递给 React Navigation 导航器内的组件? - How to pass props to component inside a React Navigation navigator? 如何使用 React 在提交时重定向到带有 props 的新组件 - How to redirect to new component with props on submit using React 通过选项卡导航器将子组件的 state 作为道具发送到另一个组件 - send child component's state to another component as props via tab navigator in react native React Native:使用Tab Navigator将组件状态发送到其他组件 - React Native: Send component state to other component using Tab Navigator 如何在 React Native 中将道具传递给模态组件 - How to Pass Props to Modal Component in React Native 如何在React Native中使用Navigator从主要组件替换特定的子组件 - How to replace particular sub component from the main component using Navigator in react native
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM