我正在尝试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
返回无状态功能组件?我真的不明白这个警告的含义。很想在这里得到一些光!
顺便说一下,派生状态正在正确更新。