内容简介:源码:课程地址:如果比较着急想了解一些原理可以先看这篇文章
04.redux整体感知
源码: https://github.com/wsdo/redux...
课程地址: https://segmentfault.com/ls/1...
如果比较着急想了解一些原理可以先看这篇文章
https://segmentfault.com/a/11...
// 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' })
通过 store.getState() 获取当前的状态
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网
猜你喜欢:本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
A Byte of Python
Swaroop C H / Lulu Marketplace / 2008-10-1 / USD 27.98
'A Byte of Python' is a book on programming using the Python language. It serves as a tutorial or guide to the Python language for a beginner audience. If all you know about computers is how to save t......一起来看看 《A Byte of Python》 这本书的介绍吧!