我正在创建一个react / Gatsby网站,并试图使导航工作需要一些指导
所以基本上,我在类似https:///mysite.com
的域上有一个网站在网站中导航时,我的路线会添加到域中,例如:https // mysite.com / page1, https // mysite.com / page2等。
刷新浏览器时,我希望网站将网站重新加载到其原始状态,即https // mysite.com。
例如,当我从/ page2重新加载时,该网站似乎记住了最后一个位置。因此,在重新加载时,地址栏中仍然有https // mysite.com / page2,而我希望网站转到主页。
这可能吗?
谢谢
答案 0 :(得分:0)
有关如何检测页面重新加载(例如按F5)的信息,请参见此question。提议第二高的答案推荐使用以下代码来检测刷新并触发另一个功能:
componentDidMount() {
window.onbeforeunload = function() {
this.onUnload();
return "";
}.bind(this);
}
盖茨比在幕后使用@reach/router
。因此,使用Gatsby Link重定向到您的根页面:
import { navigate } from "gatsby"
componentDidMount() {
window.onbeforeunload = function() {
this.onUnload();
return "";
}.bind(this);
navigate("/"); // redirect to your root page here
}
用户将能够返回上一页。如果您不想这样做,replace the history会这样:
navigate("/", { replace: true });