老师,帮忙看下这个报错原因是什么?不影响程序运行,但是想解决,是在添加persist后出现的。
来源:12-2 【路由进阶】私有路由搭建

文文文文礼
2021-02-09
A non-serializable value was detected in an action, in the path: register
. Value: ƒ register(key) {
pStore.dispatch({
type: constants__WEBPACK_IMPORTED_MODULE_1[“REGISTER”],
key: key
});
}
Take a look at the logic that dispatched this action: Object
type: "persist/PERSIST"
register: ƒ register(key)
rehydrate: ƒ rehydrate(key, payload, err)
proto: Object
(See https://redux.js.org/faq/actions#why-should-type-be-a-string-or-at-least-serializable-why-should-my-action-types-be-constants)
(To allow non-serializable values see: https://redux-toolkit.js.org/usage/usage-guide#working-with-non-serializable-data)
2回答
-
hello 新年快乐啊。你观察的非常仔细,这个问题是因为 Redux Toolkit 的中间件会深度检查状态树, 一旦检测到非序列化的数据则会报错。我们来看看redux tookit官网是怎么说的:https://redux-toolkit.js.org/api/getDefaultMiddleware
还记得课程中怎么描述redux-persist的数据保存方式吗?我们把数据以键值对的方式保存在local storage中,而真正储存的数据是stringfy以后的字符串,当我们需要使用的时候再把string从local storage中取出来,然后再转化为对象。既然保存的数据是string,所以会有这个报错。
改正方法:
在 getDefaultMiddleware 参数中设置 serializableCheck 为 false,报错就消失了。
祝你新年快乐、万事如意啊
222021-03-21 -
慕用4509656
2022-06-16
因为redux-persist定义的action中的payload包含函数类型, redux建议payload是可序列化的。直接忽略persist的type就可以消除这个报错
00
相似问题