补液后Redux Persist状态为空

时间:2018-08-09 13:27:01

标签: react-native redux redux-persist

我面临一个奇怪的问题。补液在我的应用程序中起作用,直到我切换到在本地服务器上运行的远程开发工具为止。现在我可以看到确实调用了REHYDRATE,但是之后我的所有状态仍然为空!这可能是什么原因?如果我不使用devtools,那么它也不起作用。默认情况下,我所有的减速器都返回未修改的状态。这是配置文件:

`

const rootReducer = combineReducers({
    appState: appStateReducer,
    entries: entriesReducer,
    listHelper: listHelperReducer
})

const persistConfig = {
    key: 'root',
    storage,
    stateReconciler: autoMergeLevel2
}

const pr = persistReducer(persistConfig, rootReducer)

const configureStore = () => {
    const store = createStore(pr, devToolsEnhancer({ realtime: true, hostname: 'localhost', port: 8082 }))

    const persistor = persistStore(store)
    return { persistor, store }
}

export default configureStore

还有其他人遇到类似的问题吗?

减速器:

const initialState = {

loggedInUser: 0,
pickedEntry: null,
bulkPicker: false,
bulkPicked: []
}
const appStateReducer = (state = initialState, action) => {
    switch (action.type) {

    case LOG_IN:
        return {
            ...state,
            loggedInUser: action.user
        }
    case PICK_ENTRY:
    return{
        ...state, 
        pickedEntry: {...action.entry}
    }
    case ACTIVATE_BULK_PICKER:
    return{
        ...state,
        bulkPicker: true
    }
    case DEACTIVATE_BULK_PICKER:
    return{
        ...state,
        bulkPicker: false,
        bulkPicked: []
    }
    case ADD_TO_BULK_PICKED:
    return{
        ...state,
        bulkPicked: [...state.bulkPicked, action.entry]
    }
    case REMOVE_FROM_BULK_PICKED:
    //remove from list
    //check if anything left then change state of bulkPicker
    let index = state.bulkPicked.findIndex((entry) => entry.key === action.entry.key)

    return{
        ...state,
        bulkPicked: [
            ...state.bulkPicked.slice(0, index),
            ...state.bulkPicked.slice(index+1)
        ]
    }

    default:
        return state
    }
}

export default appStateReducer





const initialState = {
workingCopy : [],
serverCopy: [],
pendingOperations: [],
activities: [],
issueIds: []
}
const entriesReducer = (state = initialState, action) => {
    switch (action.type) {
    case SET_SERVER_COPY:
        return {
            ...state,
            serverCopy: [...action.data]
        }
    case SET_WORKING_COPY:
    return {
        ...state,
        workingCopy: [...action.data]
    }
    case NEW_ENTRY:
    return {
        ...state,
        workingCopy: [...state.workingCopy, action.entry]
      }
    case STOP_ENTRY:
      const updatedList = listHelperReducer(null, {
        ...action,
        type: "END_ENTRY_UPDATE_LIST",
        entry: action.entry,
        list: state.workingCopy
      })
      return{
          ...state,
          workingCopy: [...updatedList]
      }
    case ADD_ACTIVITIES:
    return{
        ...state,
        activities: [...action.list]
    }
    case ADD_ISSUEIDS:
    return{
        ...state,
        issueIds: [...action.list]
    }

    default:
        return state
    }
}

export default entriesReducer






const listHelperReducer = (state = [], action) => {
    switch (action.type) {
    case END_ENTRY_UPDATE_LIST:

    //find index
    const index = action.list.findIndex(
        entry => entry.key === action.entry.key
      )

    //modify list
    let listToReturn = [];
    if(index === 0)
    {
        listToReturn = [
            {
                ...action.list[0],
                endDateTime: moment(),
                totalTime: getTotal(action.list[0].startDateTime.valueOf(), moment().valueOf())
            },
            ...action.list
        ]
    }
    if(index > 0 )
    {
        listToReturn = [
            ...action.list.slice(0, index),
        {
          ...action.list[index],
          endDateTime: moment(),
          totalTime: getTotal(action.list[index].startDateTime.valueOf(), moment().valueOf())
        },
        ...action.list.slice(index + 1)
        ]
    }
    //return
     return listToReturn   

    default:
        return state
    }
}

export default listHelperReducer

1 个答案:

答案 0 :(得分:0)

对于试图解决类似问题的任何人: 我的问题是由于我的应用中使用了计时器(多个setInterval函数)引起的。这可能会导致调试器随时间推移,这显然导致状态无法持久。重新启动并重建应用程序可以解决所有问题。