How do I setState within React's recompose's lifecycle method?

后端 未结 1 1376
暖寄归人
暖寄归人 2021-02-13 04:31

I am using recompose in my React project https://github.com/acdlite/recompose/

It\'s a great library. I\'m using the compose utility as a container componen

相关标签:
1条回答
  • 2021-02-13 05:10

    Well I found out if you move the lifecycle method after the withState methods, you have access to the setters by accessing this.props.setterFunction. In my case, here is the solution I was looking for:

    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: () => {
          ...
    
    0 讨论(0)
提交回复
热议问题