简体   繁体   English

Axios React/Redux - 使用 thunk 动作创建器时不触发?

[英]Axios React/Redux - not firing when using thunk action creator?

I am trying to integrate thunk in effort to get redux to allow axios to make asynchronous calls to a back end API i have.我正在尝试集成 thunk 以使 redux 允许 axios 对我拥有的后端 API 进行异步调用。

This is an existing project that is being moved over to adding in redux and typescript.这是一个现有项目,正在移至添加 redux 和 typescript。

For some reason I cannot get the endpoint to be hit when using the recommend thunk action creator like this:由于某种原因,当使用这样的推荐 thunk 动作创建器时,我无法命中端点:

export const getAllCharacters = () => {
return (dispatch: Dispatch) => {
    axios.get(charactersAPI())
    .then(response => {
        dispatch(loadCharacters(response.data));
    })
}

} }

If I remove the return (dispatch) = () => {} portion out and just have the axios request it works and hits the endpoint, however I cannot update anything.如果我删除了 return (dispatch) = () => {} 部分并且只有 axios 请求它可以工作并到达端点,但是我无法更新任何内容。

I have thunk installed and can seemingly access other portions of the state no issues, I just cannot get this async portion to trigger.我已经安装了 thunk 并且似乎可以访问 state 的其他部分没有问题,我只是无法触发这个异步部分。

store:店铺:

const store = createStore(
appReducers, 
compose((window as any).__REDUX_DEVTOOLS_EXTENSION__ && (window as any).__REDUX_DEVTOOLS_EXTENSION__(),
applyMiddleware(thunk)));

I have a feeling this is being more difficult because I am attempting to both convert over to typescript and add in redux at the same time.我觉得这更困难,因为我试图同时转换为 typescript 并添加 redux。

Any help would be appreciated, thanks.任何帮助将不胜感激,谢谢。

Try wrapping getAllCharacters inside mapDispatchToProps similar to below尝试将getAllCharacters包装在类似于下面的mapDispatchToProps

mapDispatchToProps = (dispatch: Dispatch) => ({
  getAllCharacters: (...) => dispatch(getAllCharacters(...))
})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM