Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

[RN][Redux-Persist] AutoRehydrate is not a function

I am using redux-persist 5.5.0 When I debug my react native app, Error say "autoRehydrate is not a function" my source code here, give me help please

"use strict";

import thunk from "redux-thunk";
import analytics from "./analytics";
import array from "./array";
import promise from "./promise";
import reducers from "../reducers";
import { createLogger } from "redux-logger";
import { applyMiddleware, createStore, compose } from "redux";
import { persistStore, autoRehydrate } from "redux-persist";
import { ensureCompatibility } from "./compatibility";
import { AsyncStorage } from "react-native";

const isDebuggingInChrome = false;

const logger = createLogger({
    predicate: (getState, action) => isDebuggingInChrome,
    collapsed: true,
    duration: true
});

const middleware = applyMiddleware(thunk, promise, array, analytics, logger);

async function configureStore(onComplete: ?() => void) {
    const didReset = await ensureCompatibility();
    const store = createStore(reducers, { /* TODO: Initial state */  }, compose(middleware, autoRehydrate()));

    persistStore(store, { storage: AsyncStorage }, _ => onComplete(didReset));

    if (isDebuggingInChrome) {
        window.store = store;
    }
    return store;
}
like image 215
JaeWang Lee Avatar asked Jan 30 '18 04:01

JaeWang Lee


2 Answers

redux-persist 5.x has changes in API and autoRehydrate no longer been used. Below is the way I use redux-persist now.

import React, {Component} from 'react';
import {Provider} from 'react-redux';
import {createStore, applyMiddleware, compose} from 'redux';
import {PersistGate} from 'redux-persist/lib/integration/react';
import {persistStore, persistReducer} from 'redux-persist';
import storage from 'redux-persist/lib/storage';
import Thunk from 'redux-thunk';
import Router from './Router';
import reducers from './reducers';

const persistConfig = {
    key: 'root',
    storage: storage,
};
const persistedReducer = persistReducer(persistConfig, reducers);

const store = compose(persistedReducer, {}, composeEnhancers(applyMiddleware(Thunk)));

class App extends Component {
    render() {
        const persistor = persistStore(store);
        return (
            <Provider store={store}>
                <PersistGate persistor={persistor}>
                    <Router />
                </PersistGate>
            </Provider>
        );
    }
}

export default App;
like image 79
Sagar Khatri Avatar answered Sep 19 '22 17:09

Sagar Khatri


If using the above method, the error: Object(...) is not a function comes then try using: const store = createStore( persistedReducer, applyMiddleware(thunk ,logger) );

like image 26
Aakarshit Avatar answered Sep 17 '22 17:09

Aakarshit