【问题标题】:Why can't I add to props programmatically in react native?为什么我不能在 react native 中以编程方式添加道具?
【发布时间】:2015-11-05 07:02:16
【问题描述】:

使用 react-native navigator 时 我正在使用这样的 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
                  // I am trying to move the response data to props because I am using this data in my saveRecord function but it isn't showing up there.
                 }
              }
              render: function(){
                        <View>
                          <TouchableHighlight style={styles.touchButtonBorder} underlayColor="#ffa456" onPress={saveRecord.bind(this,this.props)}>
                            <Text style={styles.button}>SAVE</Text>
                          </TouchableHighlight>
                        </View>
              }
        })

为什么不给道具分配数据?

【问题讨论】:

  • props 是只读的。
  • 但是在 reactjs 中它可以工作。但是为什么 ti 不适用于 react native

标签: javascript react-native


【解决方案1】:

@zvona 是对的,props 是只读的。不知道你是如何让它在 reactjs 中工作的,但你不应该写这样的道具。如果您需要这样做,请使用state

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-04-09
    • 1970-01-01
    • 2014-11-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-14
    相关资源
    最近更新 更多