kenberkeley / redux-simple-tutorial

Redux 简明教程。本教程深入浅出,配套入门、进阶源码解读以及文档注释丰富的 Demo 等一条龙服务

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

你好,最简单例子里面有一个reduer的**不符合redux的规范,不改变state,而是创造一个新的state

QRL909109 opened this issue · comments

https://github.com/kenberkeley/redux-simple-tutorial#-最简单的例子--在线演示-
`function reducer(state, action) {
// 首次调用本函数时设置初始 state
state = state || { counter: 0 };

switch (action.type) {
case 'INCREMENT':
return { counter: state.counter + 1 };
case 'DECREMENT':
return { counter: state.counter - 1 };
default:
return state; // 无论如何都返回一个 state
}
}`
counter:state.counter+1 应该 state{...state, counter : state.counter+1 }
虽然对于初级者直接修改来的直观些,但必要注明一下

@QRL909109 您确定这是直接修改吗?

var state = { counter: 0 }
var nextState = { counter: state.counter + 1 }

console.log(state) // { counter: 0 }
console.log(nextState) // { counter: 1 }