04.redux整体感知
如果比较着急想了解一些原理可以先看这篇文章
https://segmentfault.com/a/1190000015754271
// reducer
const weight = (state = 160, action) => {
switch (action.type) {
case 'eat':
return state + 10
case 'hungry':
return state - 10
default:
return 160
}
}
const store = createStore(weight)
console.log(store.getState())
store.dispatch({ type: 'eat' })
console.log('我吃了一些事物')
console.log(store.getState())
console.log('我饿了好几天')
store.dispatch({ type: 'hungry' })
console.log(store.getState())
console.log('我又饿了好几天')
store.dispatch({ type: 'hungry' })
console.log(store.getState())
reducer 里面使用switch语句根据传入的类型,输出新的状态
把reducer 传入 createStore(weight)
通过 dispatch 传入不同的类型,改变状态state。
store.dispatch({ type: 'hungry' })