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

accessing a prop found in either reducer #1713

Closed
Kannaj opened this issue May 11, 2016 · 1 comment
Closed

accessing a prop found in either reducer #1713

Kannaj opened this issue May 11, 2016 · 1 comment
Labels

Comments

@Kannaj
Copy link

Kannaj commented May 11, 2016

Hello,

sorry if this has already been asked earlier , but i have a prop which can be mutated by 2 reducers. I pull the isFetching variable in my component like the below

function mapStateToProps(state){
  const {errorMessage,isFetching} = state.signUp
  const {isAuthenticated,username} = state.login;
  return {
    isAuthenticated,
    errorMessage,
    isFetching,
    username
  }
}

i have two reducers

export const signUp = (state={},action) => {
  switch(action.type){
    case SIGNUP_REQUEST:
      return Object.assign({},state,{
        isFetching: true
      })
    case SIGNUP_FAILURE:
      return Object.assign({},state,{
        isFetching:false,
        // isAuthenticated: false,
        errorMessage: action.message
      })
    default:
      return state
  }
}

export const login = (state={isAuthenticated:false},action) => {
  switch(action.type){
    case LOGIN_REQUEST:
      return Object.assign({},state,{
        isFetching: true,
        isAuthenticated: false
      })
    case LOGIN_SUCCESS:
      return Object.assign({},state,{
        isFetching:false,
        isAuthenticated:true,
        username: action.username
      })
    case LOGIN_FAILURE:
      return Object.assign({},state,{
        isFetching: false,
        isAuthenticated: false,
        errorMessage: action.message
      })
    default:
      return state
  }
}

when i dispatch a loginRequest - the reducer turns isFetching to true. but the component still doesnt update because its looking at the isFetching variable only from the signUp Reducer.

is there a way i can have the component look at both reducers for the isFetching variable?

Stack overflow suggested that i do this

const isFetching = {signUp.isFetching || login.isFetching}

but is there a better way i can structure my reducers.

@gaearon
Copy link
Contributor

gaearon commented May 11, 2016

const isFetching = signUp.isFetching || login.isFetching

looks perfectly reasonable to me.

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

No branches or pull requests

2 participants