[英]How I reload the page when I click Link component in react?
I'm making a Logout function with reactJS.我正在使用 reactJS 制作注销功能。 When I click Logout button, the token of the localstorage is deleted.
当我点击注销按钮时,本地存储的令牌被删除。 But, although the localstroage is deleted, my header can't notice the change before I reload the page.(When the page is root page "/". If the page is not root page, it works well.) How can I solve this??
但是,虽然删除了localstroage,但我的header在重新加载页面之前无法注意到变化。(当页面是根页面“/”时。如果页面不是根页面,它工作得很好。)我该如何解决这个??
<Link to="/" onClick={AuthenticationService.logout} className="headerBtn">
logout
</Link>
logout() {
localStorage.removeItem("authenticatedUser");
localStorage.removeItem("token");
}
Are you sure you want to reload the whole page or just render it again, following this change in state?你确定要重新加载整个页面或只是再次渲染它,按照国家这种变化?
re-render is much faster than browser reload, because it will only update the needed parts instead of reopening your whole web app again.重新渲染比浏览器重装速度更快,因为它只会更新所需要的部分,而不是再次重新打开你的整个Web应用程序。
If you do have react's state where you could keep an indication whether a user is logged in or not, then calling setState()
within your logout()
function would re-render your view.如果你有反应的状态,在那里你可以保持一个指示用户是否登录或没有,然后调用
setState()
的内logout()
函数将重新呈现你的看法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.