【问题标题】:How to write redux-persist in redux toolkit along with TypeScript如何在 redux 工具包中与 TypeScript 一起编写 redux-persist
【发布时间】:2022-01-22 07:24:39
【问题描述】:

我一直在我的下一个 js 应用程序中使用 Redux Persist,但现在我想将 redux 工具包与类型脚本一起使用。获得了在 redux 工具包中编写 redux-persist 的语法,但找不到特定于 typescript 的任何内容。请任何人提出一种使用 redux 工具包和 typescript 实现 redux-persist 的方法。 直到现在都在使用它。

import { configureStore } from '@reduxjs/toolkit'
import {
  persistStore,
  persistReducer,
  FLUSH,
  REHYDRATE,
  PAUSE,
  PERSIST,
  PURGE,
  REGISTER,
} from 'redux-persist'
import storage from 'redux-persist/lib/storage'
import { PersistGate } from 'redux-persist/integration/react'

import App from './App'
import rootReducer from './reducers'

const persistConfig = {
  key: 'root',
  version: 1,
  whitelist: [userReducer],
  storage,
}

const persistedReducer = persistReducer(persistConfig, rootReducer)

const store = configureStore({
 reducer: persistedReducer,
 devTools: process.env.NODE_ENV !== 'production',
 middleware: [thunk]
})

let persistor = persistStore(store)

ReactDOM.render(
  <Provider store={store}>
    <PersistGate loading={null} persistor={persistor}>
      <App />
    </PersistGate>
  </Provider>,
  document.getElementById('root')
)

【问题讨论】:

    标签: reactjs typescript next.js redux-toolkit redux-persist


    【解决方案1】:

    我使用 redux 工具包和 typescript,我的商店配置如下:

    import { FLUSH, PAUSE, PERSIST, PURGE, REGISTER, REHYDRATE, persistStore } from 'redux-persist';
    
    import reducer from './rootReducer';
    
    const devToolsCompose = (window as any).__REDUX_DEVTOOLS_EXTENSION_COMPOSE__;
    
    export const store = configureStore({
      reducer,
      devTools: { trace: true, traceLimit: 25 },
      middleware: [
        ...getDefaultMiddleware({
          serializableCheck: {
            ignoredActions: [FLUSH, REHYDRATE, PAUSE, PERSIST, PURGE, REGISTER]
          }
        })
      ]
    });
    
    export type RootState = ReturnType<typeof store.getState>;
    export type AppDispatch = typeof store.dispatch;
    export const persistor = persistStore(store);
    

    Redux 工具包在这里有打字稿文档:https://redux-toolkit.js.org/usage/usage-with-typescript

    【讨论】:

      猜你喜欢
      • 2021-06-03
      • 1970-01-01
      • 2022-10-14
      • 2018-06-04
      • 1970-01-01
      • 1970-01-01
      • 2018-11-09
      • 2018-06-06
      • 1970-01-01
      相关资源
      最近更新 更多