繁体   English   中英

如何在不使用`this`的情况下获得重构生命周期的道具?

[英]How to get props on recompose lifecycle without using `this`?

我正在使用eslint-plugin-immutable所以我不能使用this关键字(我可以eslint-disable-line,但我不想),所以我想知道是否有任何recompose来访问任何mount生命周期内的props,不使用this关键字。

const enhance = compose(
  lifecycle({
    componentWillMount() {
      const { props } = this // throws eslint error
      console.log(this.props); // works, throws eslint error
    },
  }),
);

您可以使用为此目的编写的with-lifecycle HOC。 你的例子:

import withLifecycle from '@hocs/with-lifecycle';

const enhance = compose(
    withLifecycle({
        onWillMount(props) {
            console.log(props);
        },
    }),
);

lifecycle不使用this关键字就无法访问props 如果你经常需要使用相同的功能生命周期方法,你可以实现自己的HOC,仍然必须使用this ,但在外面你不必写this ,比如recompose/shouldUpdate

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM