Redux的使用(一)

Redux的使用

  • store 文件如下

    import { createStore } from 'redux'
    export const store = createStore((preState = 0, { type, data }) => {
        switch (type) {
            case 'add':
                return preState + data
            case 'jian':
                return preState - data
            default:
                return preState
        }
    })
    
  • 那么如何触发 store 呢?代码如下

    store.dispatch({ type: 'add', data: 1 })
    
  • 页面渲染

    <h1>{store.getState()}</h1>
    
  • 最后还需在入口文件中加入如下代码,才能实现响应式

    store.subscribe(() => {
        root.render(
            <App />
        );
    })
    
posted @ 2024-01-25 16:15  朱在春  阅读(2)  评论(0编辑  收藏  举报