1

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

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

4

1 回答 1

0

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

于 2018-04-27T12:43:19.583 回答