鉴于one of react-redux's official hello world example,如何实现multiply
减速器?他们实现了一个增加两个数字的减速器,但是看到一个输入作为乘数的减速器也是有益的。我知道这是非常基本的,但它是我另一个项目的分解版本。向最简单的Redux添加乘法减速器示例
这里是我在使这项工作的尝试:
const MULTIPLY_ACTION = 'MULTIPLY_ACTION'
function multiplyAction(integer) {
return {
type: MULTIPLY_ACTION,
integer
}
}
export function multiplier(state = { integer: 0 }, action) {
switch() {
case MULTIPLY_ACTION:
console.log('multiplying', action)
return {
multiple: state.integer * action.multiplier
}
default:
return state
}
}
问题我遇到:
- 重构,使mapStateToProps()与多个减速工作。我错过了什么? [请参阅下面的重构]
- 将
increaseAction
对象文字重构为函数(动作类型?)。在原始示例中,当我将const increaseAction = { type: 'increase' }
重构为const increaseAction =() => {type: 'increase'}
时,计数器缩减器不再被调用,并且我的应用程序默默失败(我正在使用create-react-app作为构建版本)。
[重构]。
function mapStateToProps(state) {
const { increaseAction, multiplyAction } = state
return {
increaseAction,
multiplyAction
}
}
非常感谢!