我是 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/