【问题标题】:Redux Thunk action creator not dispatching fetching apiRedux Thunk 动作创建者未调度获取 api
【发布时间】:2020-05-23 21:26:11
【问题描述】:

我创建了一个小型 react-redux 应用程序来使用 redux-thunk 中间件获取 api 数据,由于某种原因,返回 dispatch 的 action creator 函数不起作用。

动作创作者:

export const fetchUsers = () => {
  console.log('test 1');
  return dispatch => {
    console.log('test 2');
    dispatch(fetchUserRequest);
    axios
      .get("https://jsonplaceholder.typicode.com/users")
      .then(response => {
        const users = response.data;
        dispatch(fetchUserSuccess(users));
      })
      .catch(error => {
        const errorMessage = error.message;
        dispatch(fetchUsersFailure(errorMessage));
      });
  };
};

console.log('test 1') 工作但console.log('test 2') 不工作。 这是codesanbox链接

【问题讨论】:

    标签: react-redux redux-thunk


    【解决方案1】:

    你遗漏了一些东西: 在您缺少的 userTypes 中,当您创建 const 类型时没有 _ 例如 export const FETCH_USER_REQUEST = "FETCH USER REQUEST"; 应该是 export const FETCH_USER_REQUEST = "FETCH_USER_REQUEST"; 也在 userActions 中从 userTypes 导入它而不是 userReducers 它应该是 import { FETCH_USER_REQUEST, FETCH_USER_SUCCESS, FETCH_USER_FAILURE } from "./userTypes";

    我还修复了你的 userContainer,codesandbax:codesandbax

    【讨论】:

    • 您在 UserContainer.js 文件中进行了另一项更改。我没有注意到它,因为你没有在评论中提到。现在可以了,谢谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-06-18
    • 2020-08-18
    • 2020-11-21
    • 2019-11-12
    • 2019-03-20
    • 2019-08-14
    • 1970-01-01
    相关资源
    最近更新 更多