首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >withRouter HOC和getDerivedStateFromProps

withRouter HOC和getDerivedStateFromProps
EN

Stack Overflow用户
提问于 2018-04-27 16:41:19
回答 1查看 1.1K关注 0票数 1

我尝试在一个组件中使用getDerivedStateFromProps,如下所示:

代码语言:javascript
复制
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返回的是无状态函数组件吗?我真的不明白这个警告的意思。会很想在这里得到一些阳光!

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

EN

回答 1

Stack Overflow用户

发布于 2018-04-27 20:43:20

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50058555

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档