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
},
}),
);
source to share
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);
},
}),
);
source to share
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
.
source to share