Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Not rehydrating/persisting some parts of state #350

Closed
atasmohammadi opened this issue May 10, 2017 · 4 comments
Closed

Not rehydrating/persisting some parts of state #350

atasmohammadi opened this issue May 10, 2017 · 4 comments

Comments

@atasmohammadi
Copy link

atasmohammadi commented May 10, 2017

Hi.

I'm using redux with redux-persist and redux-thunk.

Only some part of state is not being persisted. what could be the reason? is it a way to force persist the current state? When I call reducers, state gets updated. but when I reload the app, some part of state is still the old one. (empty)

I thought it is taking some parts of state from initialstate, so I added some entries to initialstate, even so, it's returning empty objects. not getting them from initialState.

Thanks in advance.

only discussionStudents gets persisted

Store setup:

import React from "react";
import { View, AsyncStorage } from 'react-native';
import { applyMiddleware, createStore, compose } from 'redux';
import { Provider } from 'react-redux';
import { persistStore, autoRehydrate } from 'redux-persist';
import thunk from 'redux-thunk';
import { createLogger } from 'redux-logger';
import { reducer } from './reducers';
import Container from './Container';

const middlewares = [thunk];

const logger = createLogger();
middlewares.push(logger);

const store = createStore(reducer, compose(
  applyMiddleware(...middlewares)
), autoRehydrate({ log: true }));

persistStore(store, {storage: AsyncStorage});

const Root = () => (
  <Provider store={store}>
    <Container />
  </Provider>
);

export default Root;

parts of the reducer :

import {REHYDRATE} from 'redux-persist/constants';
export const types = {
    SYNCHRONISE_DISCUSSIONS: 'SYNCHRONISE_DISCUSSIONS'
};
export const actionCreators = {
    synchroniseDiscussions: (args) => {
        return dispatch => {
            /// Call API
            synchroniseDiscussionsAPI()
            .then((res) => {
                return dispatch(synchroniseDiscussions(res))
            })
            .catch((e) => {
                console.log(e)
            })

        }
    }
}
const synchroniseDiscussions = (args) => {
    return {type: types.SYNCHRONISE_DISCUSSIONS, payload: args}
}
const initialState = {
    rehydrated: false,
    discussionStudents: [],
    discussionGroups: [],
    discussionsMessages: [],
    discussionParticipants: []
}
export const reducer = (state = initialState, action) => {
    const {
        discussionStudents,
        discussionGroups,
        discussionsMessages,
        discussionParticipants
    } = state;
    const {type, payload} = action;
switch (type) {
case types.SYNCHRONISE_DISCUSSIONS:
            {
                const oldStudents = discussionStudents
                const newStudents = payload.discussionStudents
                var parsedStudents = []

                oldStudents.forEach((old, i)=>{
                    if(newStudents.findIndex(newstd => newstd.userId == old.userId) < 0){
                        parsedStudents.push({
                            ...old,
                            status: 'inactive'
                        })
                    }
                })

                newStudents.forEach((newStudent, i)=>{
                    if(parsedStudents.findIndex(pstd => pstd.userId == newStudent.userId) < 0){
                        parsedStudents.push({
                            ...newStudent,
                            status: 'active'
                        })
                    }
                })
                var newdiscussionParticipants = payload.discussionParticipants
                var newdiscussionGroups = payload.discussionGroups

                return Object.assign({}, state, {
                    discussionStudents: parsedStudents,
                    discussionParticipants: newdiscussionParticipants,
                    discussionGroups: newdiscussionGroups
                })
            }
case REHYDRATE:
            {
                return {
                    ...state,
                    rehydrated: true
                }
            }
    }
    return state
}
@atasmohammadi atasmohammadi changed the title Force persist the state after change Not rehydrating when device is offline May 10, 2017
@atasmohammadi atasmohammadi changed the title Not rehydrating when device is offline Not rehydrating/persisting some parts of state May 10, 2017
@franrios
Copy link

Same issue here

@atasmohammadi
Copy link
Author

In my case, the problem was from my side.

@franrios
Copy link

franrios commented May 22, 2017

@ataomega could you please tell us more about your problem?

@atasmohammadi
Copy link
Author

@franrios it was about Loops. the loop was skipping to next item in the loop, before the operations on the last item finishes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants