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


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

查看所有标签

猜你喜欢:

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

A Byte of Python

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》 这本书的介绍吧!

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

Markdown 在线编辑器

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

HEX CMYK 互转工具

HSV CMYK 转换工具
HSV CMYK 转换工具

HSV CMYK互换工具