簡體   English   中英

this.props.dispatch()使用重構生命周期({})未定義

[英]this.props.dispatch() undefined using recompose lifecycle({})

所以我試圖使用Recompose將我的生命周期方法移動到HOC裝飾器中。 像這樣......

export const fetchOptions= lifecycle({  
  componentDidMount() {
     this.props.dispatch(change('mainForm', 'orderHeader.proxies', this.props.currentSalesRepId));
  },

  componentDidUpdate(prevProps) {
    if (this.props.total != prevProps.total){
      this.props.dispatch(change('mainForm', 'totalPurchase', this.props.total));
    }
  }
 });

然后我試圖將它添加到我的表單組件,它將呈現我的所有標記。 像這樣。

export default compose(
  connect(mapState, mapDispatch),
  fetchOptions,
)(MainReduxForm)

我一直收到一個錯誤,指出this.props.dispatch不是一個函數......有什么想法嗎? 我故意保留這個簡短的內容以避免文字牆。 如果您需要其他信息,請告訴我們!

這里的問題是,如果您不提供mapDispatchToProps函數或在您的情況下, dispatch方法將僅自動添加到您的道具。 mapDispatch

所以你有兩個選擇,從你的連接調用中刪除mapDispatch

export default compose(
  connect(mapState),
  fetchOptions,
)(MainReduxForm)

或者,如果您在mapDispatch中執行某些mapDispatch只需將調度添加到返回對象即可

const mapDispatchToProps = (dispatch) => {
    return {
        dispatch,
        ...
        other dispatch functions here
        ...
        }
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM