javascript - 如何在 React Native 中使用导航器向组件动态添加新 Prop

标签 javascript jquery reactjs react-native

在我的场景中,我使用导航器渲染组件。在该组件中,我执行了一些操作,例如保存记录,然后我向现有组件动态添加了一个新 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 上

最佳答案

你有两个选择

  1. 使用state
  2. 使用类似 redux 的框架

    1. 因为你不能设置 props 你可以使用 this.setState 来设置加载的数据

    2. 如果您使用 redux,您可以在安装时分派(dispatch)一个操作,然后更改商店以包含加载的 Prop 。

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

关于javascript - 如何在 React Native 中使用导航器向组件动态添加新 Prop ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33536870/

相关文章:

javascript - 如何在执行一次后停止javascript中的focus()函数?

node.js - 无法在 Windows 10 上安装 create-react-app

javascript - 三.MTLLoader 不加载纹理图像

javascript - AJAX 完成对象字面量后执行函数

javascript - 返回根据查询参数匹配的数据

javascript - 加入三个变量,然后用正则表达式提取

javascript - jQuery - 观察元素高度变化

php - 在 PHP 中使用 ' echo ' 时,jquery 仅适用于顶级 SQL 搜索结果

javascript - 如果任何依赖项在路径中有 "Could not locate module",则开 Jest "src"

javascript - Cookies.set 没有设置 cookie 中的值(js-cookie)