我将一些React JS组件重构为ES6样式类,来自之前使用React.createClass的实现 . 在旧的实现中,this.setState按预期工作 .

但是,在扩展React.Component时,看起来setState方法根本不可用,即使它是作为API的一部分记录的:https://facebook.github.io/react/docs/component-api.html

是否有其他一些更新组件状态的机制,或者这更可能是我的实现的问题 .

function getState() {
  return {test: TestStore.getTest()}
}

class MyComponent extends React.Component {

  constructor(props) {
    super(props);
    this.state = getState();
    this._onChange = this._onChange.bind(this);
   }
   componentDidMount() {
     TestStore.addChangeListener(this._onChange);
   }
   _onChange() {
     this.setState(getState());
   }
    render(){
        return <div>{this.state.test}</div>
    }
}

我正在使用react js版本0.14.7

Edit :上面的剪辑实际上是正确的 . 问题是由于我身边的域特定错误