生命周期--存在期--componentWillReceiveProps
组件接收到新的props时调用,并将其作为参数nextProps使用,此时可以更改组件props及state。 之前做使用做过模拟选择实现二级联动效果.
componentWillReceiveProps (nextProps) {
if (nextProps.[所传的props]) {
this.setState({
更新state
});
}
}
react-router
1. Router组件中history属性值
**history属性,用来监听浏览器地址变化,将URL解析成一个地址对象{pathname: xxx, search:xxx}提供Router,Route中地址匹配。
var _createLocation = function _createLocation(historyState) {
var key = historyState && historyState.key;
return (0, _LocationUtils.createLocation)({
pathname: window.location.pathname,
search: window.location.search,
hash: window.location.hash,
state: key ? (0, _DOMStateStorage.readState)(key) : undefined
}, undefined, key);
};
- browserHistory
- hashHistory
- createMemoryHistory
browserHistory
其底层调用history API,最终url路径为/xxx/xxx/xx/xx,所以在使用打包工具的时候需要额外处理,不然会有可能访问到服务器端的路径,导致返回404。在使用webpack-dev-server 加上--history-api-fallback参数就可以了,参考