[英]redux toolkit state is not changing
When I send request I got my data back in json and I can see it when console.log().当我发送请求时,我在 json 中取回了我的数据,我可以在 console.log() 中看到它。 But when I try to change my state, it's not changing.
但是当我尝试更改我的 state 时,它并没有改变。 Can you guys please help me to understand why?
你们能帮我理解为什么吗? Don't judge too hard I am still learning.
不要太苛刻地判断我还在学习。 Thank you
谢谢
Here is my code这是我的代码
export const login = createAsyncThunk(
'auth/login',
async (user) => {
try {
const response = await loginUser(user);
console.log(response.data) /// data present
return response.data
} catch (error) {
console.log(error)
}
}
)
export const authSlice = createSlice({
name: 'auth',
initialState: { user: {}, status: '', message: '', success: false, error: '' },
reducers: {
[login.pending]: (state, action) => (
state.status = 'loading'
),
[login.fulfilled]: (state, { payload }) => {
state.user = payload.user
state.status = 'success'
state.message = payload.message
state.success = true
state.error = ''
},
[login.rejected]: (state, { payload }) => {
state.user = payload
state.status = 'failed'
state.success = false
state.error = payload
}
}
})
You need use the extraReducers with builder, something like that:您需要将 extraReducers 与构建器一起使用,例如:
reducers: {
//something, we don't care it
},
extraReducers: (builder) => {
builder.addCase(login.pending, (state) => {
state.status = what you want
});
builder.addCase(login.fulfilled, (state) => {
blabla
});
... and again and again
},
see https://redux-toolkit.js.org/api/createAsyncThunk (all is on the doc, no better source for RDK)请参阅https://redux-toolkit.js.org/api/createAsyncThunk (所有内容都在文档中,没有更好的 RDK 来源)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.