【问题标题】:Retrieve recompose local dispatch function from withReducer in withHandlers在 withHandlers 中从 withReducer 中检索 recompose 本地调度函数
【发布时间】:2017-08-15 07:39:57
【问题描述】:

我有一个组件已经连接到 redux 存储,并且已经注入了 dispatch。我正在尝试使用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)

我发现当我compose:

compose(handlers, enhancer)(LayerListItem)

dispatchLocal 属性未在 handler 中定义。使用recompose 创建和绑定动作创建者以更新应用程序状态的最佳方法是什么。

【问题讨论】:

    标签: javascript reactjs recompose


    【解决方案1】:

    你应该把withReducer移动到withHandlers的右边:

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

    这样withReducer会先创建dispatch函数并附加到props,然后withHandlers就可以消费了。

    【讨论】:

      猜你喜欢
      • 2017-10-13
      • 2017-12-24
      • 2018-02-22
      • 2019-01-10
      • 2019-10-02
      • 2018-01-22
      • 2011-10-10
      • 2020-09-03
      • 1970-01-01
      相关资源
      最近更新 更多