gpt4 book ai didi

redux - 路径 'emailReducer.emails.0' 中的错误 : Invariant failed: A state mutation was detected between dispatches,

转载 作者:行者123 更新时间:2023-12-05 08:22:09 25 4
gpt4 key购买 nike

我收到关于改变状态的错误消息,但 Redux Toolkit 的目的是改变状态,所以我很困惑......

错误来自 addNewEmail,我将新电子邮件添加到使用 useSelector 调用 prevEmails 的数组中,第二个参数是一个普通的字符串。

import { createSlice } from "@reduxjs/toolkit";
import { AppThunk } from "./store";

const initialState = {
emails: [],
};

export const emailSlice = createSlice({
name: "email",
initialState,
reducers: {
setEmails: (state, action: any) => {
state.emails = action.payload;
},
},
});

export const { setEmails } = emailSlice.actions;

export const addNewEmail = (prevEmails: any, email: string): AppThunk => (
dispatch
) => {
const allEmails = prevEmails.push(email);
dispatch(setEmails(allEmails));
};

export default emailSlice.reducer;

export const selectEmails = (state: any) => state.emailReducer.emails;

最佳答案

我也遇到了同样的错误,这不是你发送行动。所有你必须在你的商店中传递这个中间件。

const store = configureStore({
reducer,
middleware: (getDefaultMiddleware) => getDefaultMiddleware({
immutableCheck: false,
serializableCheck: false,
})
})

关于redux - 路径 'emailReducer.emails.0' 中的错误 : Invariant failed: A state mutation was detected between dispatches,,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64695464/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com