Accessing a reducer state from within another reducer

旧巷老猫 提交于 2019-12-03 14:08:02

The best way to proceed is to send to Loading state reducer an information to know if the other reducer already have data. To have at the end:

const loading = (state = false, action) => {

    switch (action.type) {
    case 'GET_AUDIT_DATA':
        if(!action.dataAlreadyInitialized){
            return true
        }
    case 'GET_AUDIT_DATA_RECEIVED':  
        return false
    case 'GET_AUDIT_DATA_ERROR':
        return false
    default:
        return state
    }
}

You should have access from your action function to the application state and do:

dispatch({
  type:'GET_AUDIT_DATA',
  dataAlreadyInitialized: appState.auditData.length > 0
});
Pranesh Ravi

You can call getState() over a store to get the list of reducers and the current state inside the reducers.

  1. Import the store into auditLoading (use store to get values. Don't mutate the store)
  2. store.getState().auditLoading will give you the state of auditLoading reducer.

This approach is similar to the callback provided by redux-thunk. In which (dispatch, getState) => {} will be returned to the action.

Apurva Aggarwal
import store from '../../../redux/store'

console.log(store.getState().loginReducer.accessToken)

Through this statement we will get state of accessToken

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!