【redux教程】04.redux整体感知

栏目: 服务器 · 发布时间: 7年前

内容简介:源码:课程地址:如果比较着急想了解一些原理可以先看这篇文章

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() 获取当前的状态

【redux教程】04.redux整体感知


以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网

查看所有标签

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。

Implementing Responsive Design

Implementing Responsive Design

Tim Kadlec / New Riders / 2012-7-31 / GBP 27.99

New devices and platforms emerge daily. Browsers iterate at a remarkable pace. Faced with this volatile landscape we can either struggle for control or we can embrace the inherent flexibility of the w......一起来看看 《Implementing Responsive Design》 这本书的介绍吧!

MD5 加密
MD5 加密

MD5 加密工具

html转js在线工具
html转js在线工具

html转js在线工具

HEX HSV 转换工具
HEX HSV 转换工具

HEX HSV 互换工具