ReactJS:如何在XHR后使用react-router从一个路由重定向到另一个路由?
ReactJS:如何在XHR后使用react-router从一个路由重定向到另一个路由?
这个问题已经在这里有了答案:
我卡住了。我无法在请求后重定向我的用户。
这是我的请求(在组件中):
const onSubmit = (data) =>{ const datafull = { "title": data.title, "excerpt": "nonex5", "content": data.content, "location": data.location, "company_id": "75", "category_id": data.category_id, "date_start_at": data.date_start_at, "date_end_at": data.date_end_at, "link" : data.link, } console.log(datafull); axios({ method: 'post', url: 'url', data: datafull, }) .then(function (res) { { console.log('resolution',res.data); return }; }) .catch(function (erreur) { console.log(erreur); });}; const { register, handleSubmit, errors } = useForm();
有人有建议帮助我找到解决方案吗?
admin 更改状态以发布 2023年5月24日
你可以使用历史钩子从一个路由跳转到另一个路由。\n
import { useHistory } from 'react-router-dom'; const YourComponent = () => { const history = useHistory(); // trigger this method to go to news page function goToNews() { history.push('/company/news'); } }
\n或者你也可以使用window对象 -\n
window.location.replace("url") //or window.location.assign("url")