nickmarca / examples-redux-persist-next

This is an example how you can use NextJS v4 with reduxPersist v5
16 stars 2 forks source link

need help on create reducer #2

Open ShintaroNippon opened 5 years ago

ShintaroNippon commented 5 years ago

HI there sorry bother you....

Hi there I'm following this example of element6 https://github.com/rt2zz/redux-persist/issues/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 };

};

thanks

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

nickmarca commented 5 years ago

Hey @ShintaroNippon, sorry man but I didn't understand your doubt and your code is not very clear to me as well since I'm not using these solutions in a while. Maybe if you try to reformulate it with more details I'm able to help you.

Just to note, the community seems to be more focused on MobxJS. That's what I'm using right now.