Redux

Redux核心概念

  1. store
  2. reducers
  3. actions

actions

Actions是中包含的是我们发送到store中的数据,或者触发store中的数据更新,而且在redux中actions是store中数据更新的唯一来源。我们使用store.dispatch()来发送actions。

reducers

Redux中的action只是表明了在应用中发生了什么,但是并没有对数据state进行处理,Reducers就是用来处理数据的。

store

Redux中的actions用来表明发生了什么,Reducers基于action来对state进行更新。那么Store就是action和Reducers之间的桥梁。它的功能如下:

  • 存储应用的所有state
  • 通过其getState()方法获取state
  • 通过其dispatch(action)来更新state
  • 通过其subscribe(listener)来注册监听器
  • 取消subscribe(listener)返回的监听器
  • 替换Reducer replaceReducer()

redux的npm安装

npm i redux -D 

使用步骤

1.引入redux

import { createStore } from "redux";

2.创建一个纯函数的reducer 写业务逻辑的

const reducer = function(state, action) {
            state 一个初始的数据
                state=[{txt:'呵呵',id:0,checked:false}]

            action 执行第几个逻辑  1
                action.type
                action.payload

            switch(action){
                1:
                    第一个逻辑
                2::
                    第二个逻辑
            }
            return state;
        }

`

3.创建store

const store = createStore(reducer);
请我吃辣条吧~~
-------------本文结束感谢您的阅读-------------