【问题标题】:Redux: redux thunk never being calledRedux:redux thunk 永远不会被调用
【发布时间】:2021-02-12 13:01:40
【问题描述】:

我正在使用 redux-thunks,但在调用操作时遇到了问题。我的代码如下所示:

我的动作是这样的:

export const listTeams = (league: string) => {
    console.log('This is logged');  <------ This is logged

    return async dispatch => {

      console.log('never logged!!!!');   <------- never gets here

      await teamActions.listTeams(league)
            .then(team => {
               dispatch(setTeam(team));
            })
           .catch(error => handleErrors(error, dispatch));
      };
  };

调用它的组件如下所示:

const App = () => {
   .... code

   const fetchData = async() => {
     await listTeams('Liverpool');
   };

   ... more code
}

【问题讨论】:

    标签: reactjs redux redux-thunk


    【解决方案1】:

    你有一个返回另一个函数的函数。所以..

    await listTeams('Liverpool')();
    

    【讨论】:

    • 谢谢。这有效,但似乎很奇怪。那我写这些动作错了吗?这是正常的吗?而且,现在我在 catch 块中看到一个关于 dispatch 的错误
    • 啊,因为我用的是useDispatch所以它必须是dispatch(await listTeams('Liverpool'));
    猜你喜欢
    • 2012-10-27
    • 2013-10-12
    • 2012-03-27
    • 2013-08-29
    • 2013-11-03
    • 1970-01-01
    • 1970-01-01
    • 2015-05-06
    • 2015-10-15
    相关资源
    最近更新 更多