Reactjs 如何使用React钩子调用返回分派函数的操作?

Reactjs 如何使用React钩子调用返回分派函数的操作?,reactjs,redux,react-hooks,redux-thunk,Reactjs,Redux,React Hooks,Redux Thunk,我正在使用React类和hook。我知道我不应该这样做,但你们知道公司的遗留代码是如何工作的。我们正走向虎克,但这需要时间和努力。我们正在使用redux thunk。在分页以呈现用户时,我们有如下操作: action.js文件: export const fetchAccounts = (page, countPerPage)=>{ return function (dispatch){ dispatch(fetchAccountsInit()); return axi

我正在使用React类和hook。我知道我不应该这样做,但你们知道公司的遗留代码是如何工作的。我们正走向虎克,但这需要时间和努力。我们正在使用redux thunk。在分页以呈现用户时,我们有如下操作:

action.js文件:

export const fetchAccounts = (page, countPerPage)=>{
  return function (dispatch){
    dispatch(fetchAccountsInit());
    return axiosInstance.get(`/auth/users/fetch-users/${page}/${countPerPage}`)
     .then(res => res.data)
     .then(accounts=>{
         dispatch(fetchAccountsSuccess(accounts));
         return accounts
      })
     .catch(({response}) => {
     dispatch(fetchAccountsFail(response.data.errors));
  })
} }

之前的开发人员做了如下工作: 正在使用钩子的User.js文件:

const getUserList = () => {    
  dispatch(actions.fetchAccounts(page, countPerPage))
  .then(res => {  
      setUsers(res);
      setCountPerPage(10);
      setTotalRows(res[0].total_count);
  }).catch(err => {
    setUsers({});
  });
}

useEffect(() => {
  getUserList();
}, [page]);
代码正在运行,没有问题。但在我看来,这似乎是不对的。我相信守则应为:

export const fetchAccounts = (page, countPerPage)=>{
  return function (dispatch){
     dispatch(fetchAccountsInit());
     axiosInstance.get(`/auth/users/fetch-users/${page}/${countPerPage}`)
       .then(res => res.data)
       .then(accounts=>dispatch(fetchAccountsSuccess(accounts)))
       .catch(({response}) => {
           dispatch(fetchAccountsFail(response.data.errors));
       })
  }
}
当我调用
constgetuserlist=()=>{…
上述相同的函数时 我得到的错误为
TypeError:无法读取未定义的属性“then”


User.js文件中没有正在工作的mapDispatchToProps。它正在使用来自“react redux”的
import{useDispatch};
const dispatch=useDispatch()
如果更改操作,我想我不理解此分页应该如何工作。我正在处理的项目中的代码是正确的,或者它有一些问题?

在您更改之前,thunk返回了最终数据的承诺-您删除了
return
,现在它返回
未定义的
o一个
dispatch(thunk())。然后(…)
,该thunk仍然必须返回一个承诺。

那么,在更改操作之后,如何调用“const getUserList()”中的dispatch函数,以便我可以链接“then”。我应该调用
dispatch(thunk(actions.fetchAccounts(page,countPerPage))。然后(…)
是的,这是可能的。