首页 > 解决方案 > 在另一个动作中调用 redux 动作不起作用

问题描述

我有控制台记录错误并且错误在控制台中可见但setAlert操作没有调度,这是为什么呢?

这是身份验证操作代码

 import axios from "axios"
import {setAlert} from "../actions/alert";
import { REGISTER_FAIL } from "./conts"

export const register = ({ name, username, email, password }) => async dispatch => {
    const config = {
        headers: {
            'Content-Type': 'application/json'
        }
    }
    const body = JSON.stringify({ name, username, email, password });

    try {
    const res =   await axios.post("http://localhost:8080/signup",body,config);
        setAlert("Email has been sent..! Please Confirm Your email","success",10000) //not working
}
    catch (err) {
        console.log(err.response.data.error);
      const error = err.response.data.error;

      setAlert(error,"error",5000); //not working

      dispatch({
          type:REGISTER_FAIL
      });
    }
}

这是警报动作

    import {SET_ALERT,REMOVE_ALERT} from "./conts";
import {v4 as uuidv4} from "uuid"; 

export const setAlert =(msg,alertType,timeOut =5000)=> dispatch=>{
    const id= uuidv4(); 
    dispatch({
        type: SET_ALERT,
        payload:{msg,alertType,id}
    });

    setTimeout(()=> dispatch({type:REMOVE_ALERT,payload:id}),timeOut)

}

标签: reduxreact-reduxredux-actionsredux-reducers

解决方案


好吧,看来,我只需要发送 setAlerts


推荐阅读