1.创建store 和 使用 reducer
import { createStore } from "redux";
const reducer = (pre = {
name:'ss'
},action)=>{
const newState = {...pre}
console.log(pre)
switch(action.type){
case 'cxk':
newState.name = '鸡你太美'
return newState
case 'nazi':
newState.name = '希特勒万岁'
case 'cccp':
newState.name = '卢比扬卡包吃住'
default:
return pre
}
}
const store = createStore(reducer); //创建一个store
export default store
2.派发dispatch
const btn = ()=>{
store.dispatch({ //点击派发
type:'cxk'
})
setName('sss') //异步更新
}