作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个组件已连接到 redux
存储,并已将 dispatch
注入(inject)其中。我正在尝试使用 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(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/
我正在使用 withReducer HOC 并注意到这种行为:例如,在点击处理程序上调用它: import React from 'react' import { withReducer } from
我是一名优秀的程序员,十分优秀!