rt2zz / redux-persist

persist and rehydrate a redux store
MIT License
12.97k stars 867 forks source link

hi need help in reducer #983

Closed ShintaroNippon closed 5 years ago

ShintaroNippon commented 5 years ago

HI there sorry bother you....

Hi there I'm following this example of element6 rt2zz/redux-persist#598

and I need help in creating the ./reducer in order to save information in user.. I already have in this.props the user and dispatch... but I need help...

sorry if I'm taking to much from you but I really need this ./reducer rootreducer

let _persistor;
export default Page => withRedux(
    (initialState, { isServer }) => {
        const { persistor, store } = makeStore(initialState, { isServer })
        _persistor = persistor
        return store
    }, mapStateToProps, mapDispatchToProps
)(class DefaultPage extends React.Component {

    render() {
                if (!_persistor) return <Loading/>

        return (
            <PersistGate loading={<Loading/>} persistor={_persistor}>
                <App />
            </PersistGate>
        )
    }
}

//store.js
import { createStore } from "redux";
import rootReducer from "./reducer";
import storage from 'redux-persist/lib/storage'

function clientStore(initialState) {
    const { persistStore, persistReducer } = require('redux-persist')

    const persistConfig = {
        key: 'root',
        storage,
    }
    const persistedReducer = persistReducer(persistConfig, rootReducer)
    const store = createStore(persistedReducer, initialState)
    const persistor = persistStore(store)
    return { store, persistor }

}

export const makeStore = (initialState, { isServer }) => {

    const { store, persistor } = isServer ? { store: createStore(rootReducer, initialState) } : clientStore(initialState);

    if (module.hot) {
        module.hot.accept('./reducer', () => {
            console.log('Replacing reducer');
            store.replaceReducer(require('./reducer').default);
        });
    }

    return { store, persistor };

};

can you give me an example
thanks in advance
ShintaroNippon commented 5 years ago

HI there I already did it, I have created an action and a reducer, My doubt was calling this.props.dispatch(savemydata(data))

For now it's working but the info is spread into this props...

any help to set it in one variable ? thanks

erksch commented 5 years ago

For now it's working but the info is spread into this props... any help to set it in one variable ?

What exactly do you mean by that? To use state from your store you can inject it in to your components props via

const mapStateToProps = state => ({ 
  targetProp: state.someParticularState,
});
export default connect(mapStateToProps)(MyComponent);

Please check the react-redux docs for more information about this. But the problems you are facing do not seem to be related to redux-persist.

ShintaroNippon commented 5 years ago

problem solve you can close