我正在尝试将我正在构建的一个应用程序切换到使用 Redux Toolkit,并且在我从 createStore 切换到 configureStore 时注意到了这个错误:
A non-serializable value was detected in the state, in the path: `varietals.red.0`. Value:, Varietal {
"color": "red",
"id": "2ada6486-b0b5-520e-b6ac-b91da6f1b901",
"isCommon": true,
"isSelected": false,
"varietal": "bordeaux blend",
},
Take a look at the reducer(s) handling this action type: TOGGLE_VARIETAL.
(See https://redux.js.org/faq/organizing-state#can-i-put-functions-promises-or-other-non-serializable-items-in-my-store-state)
四处查看之后,我发现这个问题似乎与我的定制模型有关。例如,品种数组是从品种模型创建的:
class Varietal {
constructor(id, color, varietal, isSelected, isCommon) {
this.id = id;
this.color = color;
this.varietal = varietal;
this.isSelected = isSelected;
this.isCommon = isCommon;
}
}
然后用它映射一个字符串数组来创建我的 Varital 数组,这个数组进入我的状态:
// my utility function for creating the array
const createVarietalArray = (arr, color, isCommon) =>
arr.map(v => new Varietal(uuidv5(v, NAMESPACE), color, v, false, isCommon));';
// my array of strings
import redVarietals from '../constants/varietals/red';
// the final array to be exported and used in my state
export const COMMON_RED = createVarietalArray(redVarietals.common.sort(), 'red', true);
当我关闭模型并用返回一个普通对象数组的工具替换数组创建实用程序时,如下所示:
export const createVarietalArray = (arr, color, isCommon) =>
arr.map(v => ({
id: uuidv5(v, NAMESPACE),
color,
varietal: v,
isSelected: false,
isCommon,
}));
然后那个特定的减速器就出错了,但是我有这些自定义模型在我的应用程序中,在我开始把它们全部删除并重新编码之前,只是为了能够使用 Redux 工具包,我想在这里问一下,这是否真的是问题所在,在我这样做之前..。