老师,帮忙看下这个报错原因是什么?不影响程序运行,但是想解决,是在添加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回答

阿莱克斯刘

2021-02-13

hello 新年快乐啊。你观察的非常仔细,这个问题是因为 Redux Toolkit 的中间件会深度检查状态树, 一旦检测到非序列化的数据则会报错。我们来看看redux tookit官网是怎么说的:https://redux-toolkit.js.org/api/getDefaultMiddleware

//img.mukewang.com/szimg/6026e8f809c54d5c18281606.jpg


还记得课程中怎么描述redux-persist的数据保存方式吗?我们把数据以键值对的方式保存在local storage中,而真正储存的数据是stringfy以后的字符串,当我们需要使用的时候再把string从local storage中取出来,然后再转化为对象。既然保存的数据是string,所以会有这个报错。


  • 改正方法:

getDefaultMiddleware 参数中设置 serializableCheck 为 false,报错就消失了。

//img.mukewang.com/szimg/6026e8f80904cf9419520682.jpg

祝你新年快乐、万事如意啊

2
2
匆匆又夏天丶
感谢感谢
2021-03-21
共2条回复

慕用4509656

2022-06-16

https://img.mukewang.com/szimg/62ab107209bf79f209620574.jpg

因为redux-persist定义的action中的payload包含函数类型, redux建议payload是可序列化的。直接忽略persist的type就可以消除这个报错

0
0

React18 系统精讲 结合TS打造旅游电商平台

React18 精讲 + 结合 TS 实战 + 热门业务开发,获取必备技能

1993 学习 · 1015 问题

查看课程