El código del reductor 1 es el siguiente. Quiero llamar a otro método reductor después de la autenticación exitosa del usuario. por lo que se basa en la respuesta del reductor 1, quiero llamar al método/acción del reductor 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 } }Cuando llamo a la acción guardar datos del reductor 2 del reductor 1, no funciona. Cómo despachar la acción del reductor 2 del reductor 1.
Edición 1: mi código de middleware es el siguiente
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; }; }Despachar una acción dentro de un reductor no es un buen movimiento. Según tengo entendido, tienes que hacer algunas actualizaciones de forma sincrónica. Una forma es, una vez que se actualiza el primer reductor, donde sea que esté consumiendo ese reductor, vaya y dentro de componentWillReceiveProps o componentDidUpdate haga algo como. NOTA: antes de realizar el envío, debe importar la tienda de configuración y crear un envío constante desde la tienda.
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}) } }