6

I am unable to get react-native's this.setState(); to work.

When I run the onUpdate method, I can see the correct JSON object being returned.

The issue is when the setState function is called nothing happens and the method fails. No more code is executed below it. It also not re-render the component after the setState function.

Here is my component code below:

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>
    );
  }
});
4

2 回答 2

10

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

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

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

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

于 2015-04-13T12:24:11.533 回答
-5

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

于 2015-05-12T15:19:10.537 回答