[英]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: 我试图使用
withReducer
和withHandlers
更新组件的本地状态,如下所示:
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.