gpt4 book ai didi

javascript - 从withHandlers中的withReducer中检索重构本地调度函数

转载 作者:行者123 更新时间:2023-11-28 04:50:31 24 4
gpt4 key购买 nike

我有一个组件已连接到 redux 存储,并已将 dispatch 注入(inject)其中。我正在尝试使用 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(handlers, enhancer)(LayerListItem)

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

最佳答案

您应该将 withReducer 移至 withHandlers 的右侧:

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

这样,withReducer将首先创建调度函数并将其附加到props,然后withHandlers可以使用它。

关于javascript - 从withHandlers中的withReducer中检索重构本地调度函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42964390/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com