如何使用 react、redux、axios 响应发送到 action
How to response send using react,redux,axios to action
我想获得从 post 返回到负载的响应。如何将返回的结果发送给 action.How 我可以这样做吗?
import axios from 'axios';
import * as types from './actionTypes';
const ROOT_URL = `http://localhost:8000`;
export function addPost(title){
const url = `${ROOT_URL}/api/v1/post/`;
var request;
axios.post(url, {
title: title,
}).then(function (response) {
request = response;
})
.catch(function (response) {
request = response;
});
console.log(request);
return {
type:types.ADD_POST,
payload:request
}
}
一种方法是使用单独的服务来调用适当的操作:
类似
export function addPost(title){
return {
type:types.ADD_POST,
payload:request
}
}
export function addPostService(title){
const url = `${ROOT_URL}/api/v1/post/`;
var request;
axios.post(url, {
title: title,
}).then(function (response) {
dispatch(addPost(response.body))
})
.catch(function (response) {
request = response;
});
}
如果您想要更高级的解决方案,请查看 redux-thunk:https://github.com/gaearon/redux-thunk
我想获得从 post 返回到负载的响应。如何将返回的结果发送给 action.How 我可以这样做吗?
import axios from 'axios';
import * as types from './actionTypes';
const ROOT_URL = `http://localhost:8000`;
export function addPost(title){
const url = `${ROOT_URL}/api/v1/post/`;
var request;
axios.post(url, {
title: title,
}).then(function (response) {
request = response;
})
.catch(function (response) {
request = response;
});
console.log(request);
return {
type:types.ADD_POST,
payload:request
}
}
一种方法是使用单独的服务来调用适当的操作:
类似
export function addPost(title){
return {
type:types.ADD_POST,
payload:request
}
}
export function addPostService(title){
const url = `${ROOT_URL}/api/v1/post/`;
var request;
axios.post(url, {
title: title,
}).then(function (response) {
dispatch(addPost(response.body))
})
.catch(function (response) {
request = response;
});
}
如果您想要更高级的解决方案,请查看 redux-thunk:https://github.com/gaearon/redux-thunk