3

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

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

2 回答 2

1

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

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

const enhance = compose(
    withLifecycle({
        onWillMount(props) {
            console.log(props);
        },
    }),
);
于 2017-08-18T05:40:12.920 回答
0

props不使用this关键字 in是无法访问的lifecycle。如果你经常有相同的特性需要使用生命周期方法,你可以实现自己的 HOC,仍然必须使用this,但除此之外你不必编写this,比如recompose/shouldUpdate.

于 2017-06-29T15:30:09.740 回答