小智 5
这是我的理解,就redux而言,reducer是一个接受两个参数(状态,动作)的函数。
1. state represents the current state of the application in store
2. action represents the action that triggered
Run Code Online (Sandbox Code Playgroud)
Redux假定化简器确实接受当前状态并且不改变状态,但是根据操作类型返回新状态。如果它坚持并且不改变状态,那么它就是纯粹的还原剂。
/ **********************纯减速器的示例********************** ******* /
var initialState = {counter:0};
function counterReducer(state = initialState, action){
if (action.type === 'INCREMENT'){
// returns a new state incrementing a counter
return {counter:state.counter + 1};
}
else if (action.type === 'DECREMENT'){
// return a new state decrementing a counter
return {counter:state.counter - 1};
}
// returns the state as is
return state;
}
Run Code Online (Sandbox Code Playgroud)
每当使用相同的参数集调用上述函数时,上述函数都没有副作用,它始终返回相同的输出。
/ *********************不纯的异径管示例*********************** **** /
var initialState = {counter:0};
function counterReducer(state = initialState, action){
if (action.type === 'INCREMENT'){
// modifies state by mutating or incrementing the counter in state
state.counter++;
}
else if (action.type === 'DECREMENT'){
// modifies state by mutating or decrementing the counter in state
state.counter--;
}
// returns the state
return state;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
888 次 |
| 最近记录: |