鑑於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
}
}
非常感謝!