2017-01-14 3 views
12

は、プロジェクト https://github.com/acdlite/recompose/Reactのrecomposeのライフサイクルメソッド内でsetStateを行うにはどうすればよいですか?私は私の中で再構成使用しています

はそれは素晴らしいライブラリだ反応します。 componentDidMount

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)コール:私はそうのようなプレゼンテーションコンポーネントへの小道具としてダウン状態を渡しコンテナコンポーネントとしてcomposeユーティリティを使用しています。これは私がやりたいことですが、lifecycle/componentDidMountにはsetIsReadyへのアクセス権がありません。どのように再構築を使用して状態をcomponentDidMountから更新するのが目的の結果を達成できますか?

答えて

13

withStateメソッドの後にlifecycleメソッドを移動すると、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:() => { 
     ... 
関連する問題