javascript - 如何从 axios.get 获取请求数据到 Redux 存储

标签 javascript reactjs redux get store

我是 Redux 新手。有人可以解释一下,我如何从 Get 获取请求数据

componentDidMount() {
        axios.get('/api/v3/products', {
            params: {
                pageNumber: 1,
                pageSize: 500,
            }
        })
            .then(response => {
                this.setState({result: response.data});
            })
            .catch(function (error) {
                console.log('error TestMethod', error);
            })
            .then(function () {
                // always executed
            });
    }

并将其存储在 Redux 中

const initState = {
    products: [
        {id: 1, title: 'first'},
        {id: 2, title: 'second'},
        {id: 3, title: 'third'},
        {id: 4, title: 'fourth'},
        {id: 5, title: 'fifth'},
    ],
};

有什么方法吗?

最佳答案

在 reducer 内部,在开关中放置一个盒子,比如...

case FETCH_DATA: { 
  return {...state, data: action.payload}
}

现在您已经有了 reducer 代码。在 then block 调度操作 {type:FETCH_DATA, payload:response.data}

.then(response => {
            this.setState({result: response.data});
        })

关于javascript - 如何从 axios.get 获取请求数据到 Redux 存储,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53722950/

相关文章:

javascript - FormData 不包含按钮的值

html - 防止 <td> 中的 <em> 标签改变高度

javascript - 如何循环和创建动态 Bootstrap 网格

javascript - 验证 Redux 表单中的 URL

reactjs - react : Parent component re-renders all children, 即使那些在状态改变时没有改变的

javascript - 由 javascript 为 Google map 图例创建的 html 元素的事件监听器不起作用

javascript - 使用 js 或 jQuery 计算 td 类的总和

javascript - 从 Highstock/Highcharts 获取奇怪的错误信息

javascript - className= {'container' } 与 className ='container' 有什么区别

javascript - 复杂 promise 返回链中的 promise catch() 顺序