简体   繁体   English

从withHedler中的withReducer检索重构本地调度函数

[英]Retrieve recompose local dispatch function from withReducer in withHandlers

I have a component which is already connected to the redux store and has the dispatch injected to it. 我有一个组件已经连接到redux存储并且已经注入了dispatch I am trying to update the local state of the component using withReducer and withHandlers like this: 我试图使用withReducerwithHandlers更新组件的本地状态,如下所示:

const reducer = (state, action) => {
  switch (action.type) {
    case 'SET_ACTIVE_TAB':
      console.log('recieved action', action)
      const { activeTab } = action
      return activeTab
    default:
     return state
  }

} }

const initialState = 'actions'

const handlers = withHandlers({
  onTabClick: props => (e, { name }) => {
    const { dispatchLocal } = props
    dispatchLocal({
      type: 'SET_ACTIVE_TAB',
      activeTab: name
    })
  }
})

const enhancer = withReducer('activeTab', 'dispatchLocal', reducer, initialState)

I find that when I compose : 我发现当我compose

compose(handlers, enhancer)(LayerListItem)

The dispatchLocal prop is not defined within the handler . dispatchLocal prop未在handler定义。 What is the best way of creating and binding action creators with recompose to update the application state. 创建和绑定动作创建者的最佳方法是recompose以更新应用程序状态。

You should move withReducer to the right of withHandlers : 你应该将withReducer到右侧withHandlers

compose(
  withReducer(...),
  withHandlers(...)
)

In this way, withReducer will create the dispatch function first and append it to props , then withHandlers can consume it. 通过这种方式, withReducer将首先创建调度函数并将其附加到props ,然后withHandlers可以使用它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM