创建两个文件
src/store/store.js
首先在store.js文件中创建默认数据,然后定义reducer函数,让后面引入index.js文件的其他文件,在使用store.dispatch的时候能够使用,
store.dispatch(props)就是将props传入到reducer中的action,而reducer中的state就是数据,在index.js文件中可以看出,最开始是undrfined的,然后令他等于defaultsate就是默认数据,然后其返回值,也会改变数据仓库的默认值,
store.subscribe()就是对数据进行监听,数据,改变了就执行,可以返回一个函数,进行解除监听。
import {createStore} from "redux";
const defaultState ={
username:"小明",
arr:["a","b","c"]
}
const reducer = function (state=defaultState,action) {
console.log(action.value)
console.log(state)
if(action.type=="hello"){
state.username=action.value
}
else if(action.type=="hi"){
state.username="名字"
}
state = JSON.parse(JSON.stringify(state))
console.