redux-toolkit 报无法序列化Warning A non-serializable value was detected in an action

A non-serializable value was detected in an action, in the path: payload.config.adapter. Value: ƒ xhrAdapter(config) {
return new Promise(function dispatchXhrRequest(resolve, reject) {
var requestData = config.data;
var requestHeaders = config.headers;
var responseType = config.resp…
Take a look at the logic that dispatched this action: {type: “system/fetchWorkList/fulfilled”, payload: {…}, meta: {…}}
(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)

解决方案:

configureStore({
  middleware: (getDefaultMiddleware) =>
    getDefaultMiddleware({
      serializableCheck: false,
    }),
})

参考资料:
Disabling the non-serializable data warning #870

  • 6
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要实现Redux Toolkit的数据持久,你可以使用一些库或中间件来帮助你完成这个任务。下面是一种常用的方法: 1. 使用redux-persist库:redux-persist是一个用于在Redux中实现数据持久的库。它提供了一个简单的方法来将Redux状态保存到本地存储(如LocalStorage或AsyncStorage)中,并在应用程序重新加载时还原状态。 首先,安装redux-persist库: ``` npm install redux-persist ``` 然后,在你的Redux store配置中,使用`persistStore`函数来创建持久的store: ```javascript import { configureStore } from '@reduxjs/toolkit'; import { persistStore, persistReducer } from 'redux-persist'; import storage from 'redux-persist/lib/storage'; // 默认使用LocalStorage import rootReducer from './reducers'; const persistConfig = { key: 'root', storage, }; const persistedReducer = persistReducer(persistConfig, rootReducer); export const store = configureStore({ reducer: persistedReducer, }); export const persistor = persistStore(store); ``` 这里,`persistConfig`对象用于配置持久设置。你可以指定一个键(key)和存储引擎(storage),它默认使用LocalStorage。然后,使用`persistReducer`函数将根reducer与配置一起包装,创建一个持久的reducer。 最后,通过调用`persistStore`函数来创建一个持久的store,并导出store和persistor。 2. 在根组件中使用`PersistGate`组件:`PersistGate`是redux-persist提供的一个React组件,用于在应用程序加载时等待持久恢复完成。在根组件中使用`PersistGate`包裹你的应用程序组件,并传入`persistor`作为prop。 ```javascript import { PersistGate } from 'redux-persist/integration/react'; import { persistor } from './store'; ReactDOM.render( <Provider store={store}> <PersistGate loading={null} persistor={persistor}> <App /> </PersistGate> </Provider>, document.getElementById('root') ); ``` 这样,当你的应用程序重新加载时,Redux状态将从本地存储中恢复。 请注意,这只是一种实现Redux Toolkit数据持久的方法之一,你也可以使用其他类似的库或中间件来实现类似的功能。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值