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

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

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

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整体感知


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

查看所有标签

猜你喜欢:

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

产品经理的第二本书

产品经理的第二本书

[美] 哥乔斯 / 戴维侬 / 中国财政经济出版社 / 2004-4 / 39.80元

产品经理的第二本书:产品经理实战技巧指南,ISBN:9787500570950,作者:(美)琳达·哥乔斯(Linda Gorchels)著;戴维侬译;戴维侬译一起来看看 《产品经理的第二本书》 这本书的介绍吧!

在线进制转换器
在线进制转换器

各进制数互转换器

Markdown 在线编辑器
Markdown 在线编辑器

Markdown 在线编辑器

HEX CMYK 转换工具
HEX CMYK 转换工具

HEX CMYK 互转工具