import{createSlice}from'@reduxjs/toolkit'constinitialState={value: 0,}exportconstcounterSlice=createSlice({name: 'counter',
initialState,reducers: {// 自动生成 action type counter/incrementincrement: (state)=>{// Redux Toolkit allows us to write "mutating" logic in reducers. It// doesn't actually mutate the state because it uses the Immer library,// which detects changes to a "draft state" and produces a brand new// immutable state based off those changesstate.value+=1},decrement: (state)=>{state.value-=1},incrementByAmount: (state,action)=>{state.value+=action.payload},sub: (state,Action)=>{state.value-=1},},})// Action creators are generated for each case reducer functionexportconst{ increment, decrement, incrementByAmount, sub }=counterSlice.actions//异步actionexportconstsubAsync=(payload)=>{returnasync(dispatch,getState)=>{setTimeout(()=>{dispatch(sub())},1000)}}exportdefaultcounterSlice.reducer