我在我的 React 项目中使用 recomposehttps://github.com/acdlite/recompose/ https://github.com/acdlite/recompose/
这是一个很棒的图书馆。我正在使用compose
实用程序作为容器组件,将状态作为 props 传递给演示组件,如下所示:
const enhance = compose(
lifecycle({
componentDidMount() {
myCall
.getResponse([productId])
.then(products => {
setIsReady(true);
});
},
}),
withState('isAvailable', 'setIsAvailable', false),
withState('isReady', 'setIsReady', false),
mapProps(({
setIsAvailable,
setIsReady,
...state,
}) => ({
onLogoutTouchTap: () => {
...
请注意setIsReady(true)
内调用componentDidMount
。这就是我想做的,但是生命周期/componentDidMount无权访问setIsReady
。我怎样才能实现我更新状态的预期结果componentDidMount
与重组?
好吧,我发现如果你移动lifecycle
后的方法withState
方法,您可以通过访问来访问设置器this.props.setterFunction
。就我而言,这是我正在寻找的解决方案:
const enhance = compose(
withState('isAvailable', 'setIsAvailable', false),
withState('isReady', 'setIsReady', false),
lifecycle({
componentDidMount() {
myCall
.getResponse([productId])
.then(products => {
this.props.setIsReady(true);
});
},
}),
mapProps(({
setIsAvailable,
setIsReady,
...state,
}) => ({
onLogoutTouchTap: () => {
...
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)