Comments (11)
more specifically I think what you are asking is how to use it with applyMiddleware. both applyMiddleware and autoRehydrate are store enhancers so you can compose them as follows:
const store = createStore(
combineReducers(reducers),
{},
compose(
autoRehydrate(),
applyMiddleware(thunk)
)
)
This is using the new redux enhancer as a third argument api, so make sure your redux is up to date. 👍
from redux-persist.
store/index.js
import { createStore, applyMiddleware, compose } from 'redux';
import { persistStore, persistReducer } from 'redux-persist';
import storage from 'redux-persist/lib/storage';
import thunk from 'redux-thunk';
import createHistory from 'history/createBrowserHistory';
import rootReducer from '../reducers';
import initialState from './initialState';
import { isDevelopmentEnvironment } from '../environment';
export const history = createHistory();
const enhancers = [];
const middleware = [thunk];
const persistConfig = {
key: 'root',
storage
};
const persistedReducer = persistReducer(persistConfig, rootReducer);
if (isDevelopmentEnvironment()) {
const devToolsExtension = window.__REDUX_DEVTOOLS_EXTENSION__;
if (typeof devToolsExtension === 'function') {
enhancers.push(devToolsExtension());
}
}
const composedEnhancers = compose(
applyMiddleware(...middleware),
...enhancers
);
export default () => {
const store = createStore(persistedReducer, initialState, composedEnhancers);
return { store, persistor: persistStore(store) };
};
App.js
//@flow
// Vendors
import React from 'react';
import { Provider } from 'react-redux';
import { PersistGate } from 'redux-persist/integration/react';
// Layouts
import { LayoutGlobal } from './layouts';
// Store
import configureStore from './store';
const { persistor, store } = configureStore();
const App = () => (
<Provider store={store}>
<PersistGate loading={null} persistor={persistor}>
<LayoutGlobal />
</PersistGate>
</Provider>
);
export default App;
from redux-persist.
Thanks, but I still get an error. Says "compose is not defined"
Yes, it is not a global. It is exported from redux
, just like applyMiddleware
. Modern libraries usually don’t export globals, so if you see something that wasn’t declared, it’s very likely that it needs to be imported from somewhere.
import { createStore, applyMiddleware, compose } from 'redux'
from redux-persist.
I should note you will need to import all of those methods and also run persistStore:
import {autoRehydrate, persistStore} from 'redux-persist'
// ...
persistStore(store)
from redux-persist.
Thanks, but I still get an error. Says "compose is not defined" at the line #4 of your code snippet before. package.json says I run v4.4.0 of react-redux and v1.5.5 of redux-persist, in the same file i added the line
import { autoRehydrate, persistStore } from 'redux-persist';
before.
from redux-persist.
Works like a charm now, many thanks.
from redux-persist.
Thank you very much @rt2zz !
from redux-persist.
I've got an error 'redux-persist' does not contain an export named 'autoRehydrate'.
from redux-persist.
Same as the poster above. Maybe this should be added to the documentation.
from redux-persist.
Was anyone able to solve the autoRehydrate issue?
from redux-persist.
autoRehydrate was taken out in v5 from what I could tell, still looking for how this works with the latest version of persist.
from redux-persist.
Related Issues (20)
- Importing PersistGate results in compilation error HOT 1
- Jest snapshot is always created as null in react native
- Persist not working after page reload or refresh HOT 3
- failed to preserved initial state value at rehydrate
- PersistGate in SSR applications rendering app twice when using React v18 HOT 3
- Documentation for Migration is unnecessary complicated. HOT 1
- Getting got unexpected undefined error while changing redux state HOT 1
- getting error: A non-serializable value was detected in an action HOT 5
- createStore() is old
- Clear Redux Persist Data on App uninstall in IOS React Native HOT 1
- Support for Redux v5 and RTK v2 HOT 10
- state is null HOT 1
- data migration security and automatization
- Redux persist not working with RN 0.73 HOT 10
- Move project to new repo? HOT 13
- redux-persist failed to create sync storage. falling back to noop storage | Next js HOT 3
- Vitest - The slice reducer for key "reducer name" returned undefined during initialization
- Option to disable storage creation error for dynamic stores
- storage.getItem is not a function (it is undefined) - RN 0.73.4 HOT 3
- Redux persist getting cleared after persisting PatientsReducers
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from redux-persist.