我收到此错误:Invariant failed: A state mutation was detected inside a dispatch
并且似乎无法找到解决方案。我通过 API 获取用户支持票证并将其保存到某个状态以显示票证信息。这是我的代码:
const initialState: stateType = {
User: new User(),
SupportTickets: new SupportTickets()
}
const userSlice = createSlice({
name: "User",
initialState,
reducers: {
setUser: (state, {payload}) => {
state.User = payload
},
setSupportTickets: (state, {payload}) => {
state.SupportTickets.tickets = payload
}
}
})
export const {setUser, setSupportTickets} = userSlice.actions
export const userSelector = (state: stateType) => state.User
export const userSupportTicketsSelector = (state: stateType) => state.SupportTickets
export function fetchSupportTickets(userId: string, token: string) {
return async (dispatch: AppDispatch) => {
return await fetch(API_URL, {
method: 'GET',
headers: {
Authorization: `Bearer ${token}`
}
}).then(response => {
if (!response.ok) { throw response }
return response.json()
}).then(json => {
dispatch(setSupportTickets(json))
}).catch(error => {
console.log('Failed to fetch support tickets', error);
})
}
}
interface stateType {
User: User
SupportTickets: SupportTickets
}
export class SupportTickets {
tickets: SupportTicket[] = []
}
export class SupportTicket {
id: number = 0
type: string = ""
subject: string = ""
priority: string = ""
status: string = ""
created_at: string = ""
updated_at: string = ""
}
我尝试过改变/移动事物但没有成功。任何帮助是极大的赞赏。提前致谢。
弑天下
相关分类