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

I am using eslint-plugin-immutable

, so I canโ€™t use the keyword this

(I can use eslint-disable-line, but I donโ€™t want to), so I am wondering if there is a way with recompose

to access props inside any mount lifecycle without using the keyword this

.

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

      

+3


source to share


2 answers


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



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

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

      

+1


source


It is impossible to access props

without using the keyword this

in lifecycle

. If you often have the same function, need to use lifecycle methods, you can implement your own HOC, you still have to use this

, but outside of what you don't need to write this

, for example recompose/shouldUpdate

.



0


source







All Articles