我有一个表单组件,在填写表单并放入所有数据后,如果您按下submit,它将转到proectaction并执行axios api post调用。如果成功,我希望它返回到"/dashboard“
/* eslint-disable comma-dangle */
/* eslint-disable arrow-parens */
import axios from 'axios';
// import { useHistory } from 'react-router-dom';
import { GET_ERRORS } from './types';
const createProject = (project) => async dispatch => {
await axios.post('http://localhost:8080/api/project', project)
.catch((error) => {
dispatch({
type: GET_ERRORS,
payload: error.response.data
});
});
};
export default createProject;发布于 2019-10-17 05:22:18
你可以这样做:
编辑:确保将历史属性传递给操作,或者在组件上使用操作时进行推送。
const createProject = (project, history) => async dispatch => {
try {
await axios.post('http://localhost:8080/api/project', project)
history.push('/') // <---- Make the push here only after post success.
} catch(error) {
dispatch({
type: GET_ERRORS,
payload: error.response.data
});
}
};尝试catch block,然后将历史记录推送到不同的路由。
或者:
const createProjectHandler = async () => {
createProject(project).then(() => {
props.history.push('/')
})
}这只在返回axios调用时有效。我不确定异步和等待是否也是一样的。
https://stackoverflow.com/questions/58421733
复制相似问题