React-Native this.setState()不起作用

时间:2015-04-06 05:32:45

标签: react-native setstate

我无法让反应原生的this.setState();工作。

当我运行onUpdate方法时,我可以看到返回正确的JSON对象。

问题是调用setState函数时没有任何反应,方法失败。它下面不再执行任何代码。它也不会在setState函数之后重新呈现组件。

以下是我的组件代码:

var App = React.createClass({

  getInitialState: function() {
    return {
      view: Login,
      menu: false,
    }
  },

  componentDidMount: function() {
    var self = this;
  },

  onUpdate: function(val){
    // val is verified as an object here when I log to console
    this.setState(val);
    // nothing runs here. The above line errors out the function
  },

  render: function () {
    if(this.state.view == 'Home'){
      this.refs.sideMenu.frontView = Home;
      return (
        <View style={styles.container} >
          {sideMenu}
        </View>
      );
    }
    return (
      <View style={styles.container} >
        <Login onUpdate={this.onUpdate} />
      </View>
    );
  }

});

2 个答案:

答案 0 :(得分:7)

您的登录组件onUpdate方法可能是使用无法序列化的对象调用的。例如,它可以包含函数,也可以包含循环引用。

在onUpdate方法中,你应该从val参数中选择你感兴趣的东西,并将其插入状态。像这样:

this.setState({
userName: val.userName,
userId: val.userId
});

或发送到onUpdate的对象中包含的任何内容。

答案 1 :(得分:-4)

我能够通过使用Flux架构来纠正这个问题。我使用了这里找到的McFly包:https://github.com/kenwheeler/mcfly

相关问题