如何在React的recompose的生命周期方法中设置state?

Mar*_*ust 15 javascript lifecycle reactjs recompose

我在我的React项目中使用重构 https://github.com/acdlite/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: () => {
      ...
Run Code Online (Sandbox Code Playgroud)

请注意其中的setIsReady(true)电话componentDidMount.这是我想要做的,但是lifecycle/componentDidMount无权访问setIsReady.如何componentDidMount通过重构来完成更新状态的预期结果?

Mar*_*ust 18

好吧,我发现如果你在lifecycle方法之后移动withState方法,你可以通过访问访问setter 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: () => {
      ...
Run Code Online (Sandbox Code Playgroud)