【发布时间】:2019-04-13 23:46:29
【问题描述】:
我有两个不同的链接。一个是主页,另一个是图库。我在主页 3 上带有方法 scrollIntoView onClick 的链接,这些链接将我带到不同的部分。我想实现将我从画廊组件重定向到主页的方法,完成后调用方法scrollIntoView。
goToContact 方法:
goToContact = e => {
if (window.location.pathname === "/fotobudka/") {
this.fbContactElement.scrollIntoView({
behavior: "smooth",
block: "start"
});
}
if (window.location.pathname !== "/fotobudka") {
this.changeUrl();
this.goto();
}
};
更改网址将我重定向到主页:
changeUrl() {
return <Redirect to="/fotobudka/" />;
}
完成后:
goto = () => {
setTimeout(() => {
let fbContactElement = document.getElementById("contact-form");
fbContactElement.scrollIntoView({
behavior: "smooth",
block: "start"
});
}, 100);
};
我的方法效果很好,但我知道 SetTimeout 不是好的解决方案。我已经尝试过异步/等待,但我认为我还不足以实现它。
如何在不使用 SetTimeout 函数的情况下对其进行重构?
【问题讨论】:
-
你试过用
browserHistory吗? Check this 或 this
标签: javascript reactjs react-router