【发布时间】:2021-01-24 18:00:52
【问题描述】:
我有一个反应组件,当我单击菜单选项时会呈现,此页面有一个要完成的表单,并且已经完成,onSubmit 我想将它重定向到用户之前单击的组件。
我尝试使用 Redirect 并使用 useHistory() 来重定向它,但它们都不起作用,这就是我使用 useHistory() 所做的:
当前网址是localhost/home/schemes/firstoption,我想回localhost/home/firstoption。
Scheme.js:
let history = useHistory();
<Button
className="button-dc btn-block top30"
variant="primary"
type="submit"
disabled={enableButton}
>
Finish
</Button>
const onSubmit = async (event) => {
event.preventDefault();
event.stopPropagation();
{...}
history.push(previousURL);
};
const URL = window.location.href;
const previousURL = URL.split("scheme/").join("");
previousURL // localhost/home/firstoption
这会创建此 URL,但不会重定向:
https://localhost/home/scheme/https://localhost/home/firstoption
我也尝试做同样的事情,但没有使用
history.push(previousURL);
我用过这个:
return <Redirect to={previousURL} />
这不会创建 URL,也不会重定向。
有人看到这有什么错误吗?
【问题讨论】:
标签: javascript reactjs routes react-hooks