Skip to content

Instantly share code, notes, and snippets.

@dialguiba
Last active February 5, 2022 05:29
Show Gist options
  • Save dialguiba/6b1fd5d05e0ac0f08950f78ac9dfed3e to your computer and use it in GitHub Desktop.
Save dialguiba/6b1fd5d05e0ac0f08950f78ac9dfed3e to your computer and use it in GitHub Desktop.
react-store-persistent

React Redux Store Persistent

Packages

  • react-redux
  • react-thunk
  • react-persist
  • redux
import React from "react";
import ReactDOM from "react-dom";
import { TodoApp } from "./TodoApp";
import { store, persistor } from "./store/store";
import { Provider } from "react-redux";
/* */
import { PersistGate } from "redux-persist/integration/react";
import { HashRouter } from "react-router-dom";
ReactDOM.render(
<React.StrictMode>
<Provider store={store}>
<PersistGate loading={null} persistor={persistor}>
<HashRouter>
<TodoApp />
</HashRouter>
</PersistGate>
</Provider>
</React.StrictMode>,
document.getElementById("root")
);
import { combineReducers } from "redux";
import { todoReducer } from "./todoReducer";
export const rootReducer = combineReducers({
content: todoReducer,
// TODO: CAlendarReducer
});
import { applyMiddleware, compose, createStore } from "redux";
import thunk from "redux-thunk";
import { rootReducer } from "../reducers/rootReducer";
/* */
import { persistStore, persistReducer } from "redux-persist";
import storage from "redux-persist/lib/storage"; // defaults to localStorage for web
const persistConfig = {
key: "root",
storage,
};
const persistedReducer = persistReducer(persistConfig, rootReducer);
const composeEnhancers = (typeof window !== "undefined" && window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__) || compose;
/* export const store = createStore(rootReducer, composeEnhancers(applyMiddleware(thunk))); */
export const store = createStore(persistedReducer, composeEnhancers(applyMiddleware(thunk)));
export const persistor = persistStore(store);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment