createAsyncThunk方法中返回一个对象。 在extraReducers方法中的 [getLogin.fulfilled.type]:(state,action)=>{ console.log('action',action) state.loading = false; state.token = action.payload }, 这个action是怎么传递过来的。里面的payload又是怎么传递的
示例代码如下
export const getLogin = createAsyncThunk('signIn/getLogin', async (param:{ email:string,password:string },thunkAPI)=>{ const { data } = await axios.post('http://123.56.149.216:8080/auth/login',param) console.log(data.token) return data.token }) export const signIn = createSlice({ name:'signIn', initialState, reducers:{}, extraReducers:{ [getLogin.pending.type]:(state)=>{ state.loading = true }, [getLogin.fulfilled.type]:(state,action)=>{ console.log('action',action) state.loading = false; state.token = action.payload }, [getLogin.rejected.type]:(state)=>{ state.loading = false } } })
React18 精讲 + 结合 TS 实战 + 热门业务开发,获取必备技能
了解课程