如何多次使用Dispatch async?

How to useDispatch async multiple times?

我使用了两次 useDispatch 来更改我的待办事项,然后更改我的登录状态。两者都是分开工作的,但是当彼此放在一起时,第二次调度会将我的待办事项列表覆盖为一个空对象[]。 我将如何进行这项工作?

Axios Post

axios
      .post("http://localhost:3333/user/login", newUser)
      .then((response) => {
        //do stuff
        dispatch(changeTodos(stuff));
        dispatch(login());
      });

操作数

export const login = (data) => {
  return {
    type: "LOGIN",
    data: data,
  };
};

export const changeTodos = (data) => {
  return {
    type: "CHANGETODOS",
    data: data,
  };
};

减速机

const loggedReducer = (state = false, action) => {
  switch (action.type) {
    case "LOGIN":
      return true;

    case "LOGOUT":
      return false;

    default:
      return false;
  }
};

export default loggedReducer;

const todosReducer = (state = [], action) => {
  switch (action.type) {
    case "CHANGETODOS":
      return action.data;

    default:
      return [];
  }
};

export default todosReducer;

对于您的默认设置,您需要 return 声明,否则当它与操作类型不匹配时它将达到默认设置。例如,登录将使您的 TodosReducer return [] 这就是它被清除的原因。

    default:
      return state;