[英]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.