Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

379
Vistas
how to call another reducer method with redux react

Reducer 1 code is as below. I want to call another reducer method after successful authetication of user. so its based of response of reducer 1 , I want to call method/action of reducer 2.

const LOGIN = 'redux-example/auth/LOGIN';
const LOGIN_SUCCESS = 'redux-example/auth/LOGIN_SUCCESS';
const LOGIN_FAIL = 'redux-example/auth/LOGIN_FAIL'; 
import { browserHistory } from 'react-router';
import { apiurl } from '../../Constants';

import {savedata} from '../../redux/modules/new';

export default function reducer(state = initialState, action = {}) {
switch (action.type) {    
case LOGIN:
  return {
    ...state,
    loggingIn: true
  };
case LOGIN_SUCCESS:
  return {
    ...state,
    loggingIn: false,
    user: action.result
  };
case LOGIN_FAIL:
  return {
    ...state,
    loggingIn: false,
    user: null,
    loginError: action.error
  };
default:
  return state;
}
}

export function login(page,email,password) {
var querystring = require('querystring');
if(action == undefined) action = null;
var data = querystring.stringify({
    email: email,
    password: password
});
return {
    types: [LOGIN, LOGIN_SUCCESS, LOGIN_FAIL],
    promise: (client) => client.post(apiurl + 'ajax/login', {
       data: data
    }).then(response => {
        //console.log(response);
        switch(page){
            case 'signin':
                if(response.auth == 'true') {
                    redirectuser(response);
                }
            break;
            default:
            break;
        }
        return response;
    })
    .catch( error => Promise.reject(error))
};

}

export  function  redirectuser(response) {
console.log('response is as below');
console.log(response);
if(response.action == 'action1'){
    savedata();
    // here I want call another reducer method save data
}
}

When I call action save data of reducer 2 from reducer 1 , it does not work. How to dispatch action of reducer 2 from reducer 1.

Edit 1: my middleware code is as below

export default function clientMiddleware(client) {
return ({ dispatch, getState }) => next => action => {
if (typeof action === 'function') {
  return action(dispatch, getState);
}

const { promise, types, ...rest } = action; // eslint-disable-line no-redeclare
if (!promise) {
    return next(action);
}

const [REQUEST, SUCCESS, FAILURE] = types;
next({ ...rest, type: REQUEST });

const actionPromise = promise(client, dispatch);
actionPromise.then(
  result => next({ ...rest, result, type: SUCCESS }),
  error => next({ ...rest, error, type: FAILURE })
).catch(error => {
  next({ ...rest, error, type: FAILURE });
});

return actionPromise;
};
}
over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

Dispatching an action inside a reducer is not a good move. As i understand, you have to do some update synchronously. One way is, once the first reducer is updated, where ever your are consuming that reducer go and inside componentWillReceiveProps or componentDidUpdate do something like. NOTE: before dispatching you have to import the configurestore and create a const dispatch from store.

componentWillReceiveProps(nextProps)
 {
  //only if user was not there previously and now user is there
     if(!this.props.user && nextProps.user)
      {
         dispatch({type: SECOND_ACTION, payLoad})
     }
 }
over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda