我尝试在一个组件中使用getDerivedStateFromProps,如下所示:
class MyComponent extends React.Component {
constructor(props) {
super(props);
this.state = {
location: '',
};
}
static getDerivedStateFromProps(nextProps, prevState) {
return {
location: nextProps.location.pathname,
};
}
render() {....}
}
const EnhancedComponent = withRouter(MyComponent);
export default EnhancedComponent;并得到一个警告Stateless functional components do not support getDerivedStateFromProps.
withRouter返回的是无状态函数组件吗?我真的不明白这个警告的意思。会很想在这里得到一些阳光!
顺便说一句,派生状态正在被正确更新。
发布于 2018-04-27 20:43:20
它确实与评论中提到的吊装有关。已在react-router 4.3.0-RC2 https://github.com/ReactTraining/react-router/issues/6057中修复
https://stackoverflow.com/questions/50058555
复制相似问题