Get started with redux-persist CDN
MIT licensed
Redux-persist: Middleware saving/restoring Redux state to localStorage or alternatives.
Tags:- redux
- redux-middleware
- localstorage
- redux-persist
- redux-storage
- redux-rehydrate
Stable version
Copied!
How to start using redux-persist CDN
// Import required libraries
import { createStore, applyMiddleware } from 'redux';
import { persistStore, persistReducer } from 'redux-persist';
import storage from 'redux-persist/lib/storage'; // Defaults to localStorage if omitted
import ReduxThunk from 'redux-thunk';
// Define the reducer
const initialState = { count: 0 };
const reducer = (state = initialState, action) => {
switch (action.type) {
case 'INCREMENT':
return { count: state.count + 1 };
default:
return state;
}
};
// Configure persistReducer
const persistConfig = {
key: 'root',
storage,
};
const persistedReducer = persistReducer(persistConfig, reducer);
// Create the Redux store with middleware
const store = createStore(persistedReducer, applyMiddleware(ReduxThunk));
// Create the persistor and run it
const persistor = persistStore(store);
// Start listening for state changes and save them
persistor.subscribe((state) => {
console.log('State has been updated:', state);
});
// Export the store and persistor for usage
export { store, persistor };
Copied!
Copied!
Copied!
Copied!
All versions
4.0.0
4.0.0-alpha5
4.0.0-alpha6
4.0.0-alpha7
4.0.0-beta1
4.0.0-beta1.2
4.0.0-beta2
4.0.1
4.1.0
4.1.1
4.1.2
4.10.0
4.10.1
4.10.2
4.2.0
4.3.0
4.3.1
4.4.0
4.4.1
4.4.2
4.5.0
4.6.0
4.6.0-es
4.7.0
4.7.1
4.8.0
4.8.1
4.8.2
4.8.3
4.9.0
4.9.1
5.10.0
5.11.0-alpha
5.6.10
5.6.11
5.6.12
5.7.0
5.7.1
5.7.2
5.8.0
5.9.0
5.9.1
*** 6.0.0
6.0.0-pre1
6.0.0-pre2
6.0.0-pre2.0
6.0.0-pre2.1