正如您从文档中阅读的那样,没有其他替代方法,原因是性能提高。
但是,我假设您要在更改状态后执行一项操作,可以通过以下方法实现此目的:
class MyComponent extends React.Component { constructor(props) { super(props); this.state = { x: 1 }; console.log('initial state', this.state); } updateState = () => { console.log('changing state'); this.setState({ x: 2 },() => { console.log('new state', this.state); }) } render() { return ( <div> <button onClick={this.updateState}>Change state</button> </div> ); }}ReactDOM.render( <MyComponent />, document.getElementById("react"));<div id="react"></div><script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script><script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script>


