我是React.js的新手,我正在努力理解反应生命周期方法中的几种方法.
到目前为止,我仍然有一些让我困惑的事情:
1)
据我了解,之间的差别componentWillUpdate,并componentWillReceiveProps
是,componentWillReceiveProps当父母改变的道具,我们可以使用的setState(这里面孩子的setState会叫componentWillReceiveProps).
例如:https: //github.com/bgerm/react-table-sorter-demo/blob/master/jsx/app.jsx
var App = React.createClass({
getInitialState: function() {
return {source: {limit: "200", source: "source1"}};
},
handleSourceChange: function(source) {
this.setState({source: source});
},
render: function() {
return (
<div>
<DataSourceSelectors onSourceChange={this.handleSourceChange} source={this.state.source} />
<TableSorter dataSource={urlForDataSource(this.state.source)} config={CONFIG} headerRepeat="5" />
</div>
);
}
});
Run Code Online (Sandbox Code Playgroud)
在TableSorter中,我们有
componentWillReceiveProps: function(nextProps) {
// Load new data when the dataSource property changes.
if (nextProps.dataSource != this.props.dataSource) {
this.loadData(nextProps.dataSource);
}
}
Run Code Online (Sandbox Code Playgroud)
意思是当我们改变时this.state.source,我们期望componentWillReceiveProps …