withRouter HOC和getDerivedStateFromProps

时间:2018-04-27 08:41:19

标签: reactjs react-16

我正在尝试在组件中使用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是否返回无状态功能组件?我真的没有得到这个警告的含义。很想在这里获得一些亮点!

顺便说一下,派生状态正在适当更新。

1 个答案:

答案 0 :(得分:0)

确实与评论中提到的吊装有关。 已在react-router 4.3.0-rc.2中修复 https://github.com/ReactTraining/react-router/issues/6057

相关问题