我正在我的React项目https://github.com/acdlite/recompose/中使用recompose
这是一个很棒的库。我使用compose实用程序作为容器组件,将状态作为道具向下传递给表示组件,如下所示:
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: () => {
...注意componentDidMount中的setIsReady(true)调用。这就是我想要做的,但是lifecycle/componentDidMount不能访问setIsReady。如何通过recompose实现从componentDidMount更新状态的预期结果?
发布于 2017-01-15 00:00:31
我发现如果将lifecycle方法移到withState方法之后,就可以通过访问this.props.setterFunction来访问setter方法。在我的例子中,这是我正在寻找的解决方案:
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: () => {
...https://stackoverflow.com/questions/41651283
复制相似问题