【问题标题】:TypeError: axios__WEBPACK_IMPORTED_MODULE_1___default.a.post(...).then(...).dispatch is not a functionTypeError: axios__WEBPACK_IMPORTED_MODULE_1___default.a.post(...).then(...).dispatch 不是函数
【发布时间】:2020-10-20 12:11:46
【问题描述】:

我是 react redux 的新手,我收到类似“WEBPACK_IMPORTED_MODULE_1___default.a.post(...).then(...).dispatch is not a function”的错误 请帮忙 这是我正在使用的代码

API 平均值 == http://localhost:8000/api/

index.js

import {API} from '../backend';
import axios from 'axios';
export const getdata = (todo) => {
    return (dispatch) => {
        axios.post(`${API}addtodo`)
        .then(res => {
            console.log(res)
        })
        .dispatch({
            type : 'FETCH_TODO',
            payload : todo
        })
        .catch(err =>{
            console.log(err);
        })
    }
}

【问题讨论】:

    标签: javascript reactjs react-redux react-hooks redux-thunk


    【解决方案1】:

    您正试图以错误的方式使用调度。你应该做的是

    import {API} from '../backend';
    import axios from 'axios';
    export const getdata = (todo) => {
        return (dispatch) => {
            axios.post(`${API}addtodo`)
            .then(res => {
                console.log(res)
                dispatch({
                    type : 'FETCH_TODO',
                    payload : todo
                })
            })
            .catch(err =>{
                console.log(err);
            })
        }
    }
    

    那是因为在.then() 之后执行.dispatch() 意味着您正在调用 axios 返回的 Promise 上的调度方法。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-12-21
      • 2017-06-12
      • 2021-07-24
      • 1970-01-01
      • 2022-09-28
      • 2017-06-24
      • 2023-03-28
      • 2018-01-06
      相关资源
      最近更新 更多