简体   繁体   中英

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

I'm using eslint-plugin-immutable so I can't use this keyword (I can eslint-disable-line, but I don't want to), so I'm wondering if there is any way with recompose to access the props inside any of the mount lifecycle, without using the this keyword.

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

You can use with-lifecycle HOC which was written exactly for this purpose. Your example:

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

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

It's impossible to access props without using this keyword in lifecycle . If you frequently have the same feature need to use lifecycle methods, you can implement your own HOC, still have to use this , but outside that yout don't have to write this , like recompose/shouldUpdate .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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