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

Tim*_*OES 5 setstate react-native

我无法让反应本地人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>
    );
  }
});
Run Code Online (Sandbox Code Playgroud)

Ste*_*ten 8

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

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

this.setState({
userName: val.userName,
userId: val.userId
});
Run Code Online (Sandbox Code Playgroud)

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


Tim*_*OES -5

我能够使用 Flux 架构解决这个问题。我使用了在这里找到的 McFly 包: https: //github.com/kenwheeler/mcfly