YanoRyuichi.com/
Wiki
Blog
GitHub
Sandbox
開始行:
* Reducer [#i9e14d1e]
function reducer(state, action) {
if (action.type === "ADD") {
return { ...state, count: state.count + action.by }
} else if (action.type === "MINUS") {
return { ...state, count: state.count - action.by }
}
return state
}
let initialState = { count: 0 }
let actions = [
{ type: 'ADD', by: 2 },
{ type: 'MINUS', by: 4 },
{ type: 'ADD', by: 10 }
]
console.log(actions.reduce(reducer, initialState))
終了行:
* Reducer [#i9e14d1e]
function reducer(state, action) {
if (action.type === "ADD") {
return { ...state, count: state.count + action.by }
} else if (action.type === "MINUS") {
return { ...state, count: state.count - action.by }
}
return state
}
let initialState = { count: 0 }
let actions = [
{ type: 'ADD', by: 2 },
{ type: 'MINUS', by: 4 },
{ type: 'ADD', by: 10 }
]
console.log(actions.reduce(reducer, initialState))
ページ名: