[英]Recompose “withReducer”: justification of async reducer function call
[英]Retrieve recompose local dispatch function from withReducer in withHandlers
我有一個組件已經連接到redux
存儲並且已經注入了dispatch
。 我試圖使用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)
我發現當我compose
:
compose(handlers, enhancer)(LayerListItem)
dispatchLocal
prop未在handler
定義。 創建和綁定動作創建者的最佳方法是recompose
以更新應用程序狀態。
你應該將withReducer
到右側withHandlers
:
compose(
withReducer(...),
withHandlers(...)
)
通過這種方式, withReducer
將首先創建調度函數並將其附加到props
,然后withHandlers
可以使用它。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.