简体   繁体   English

当我在 React 中单击链接组件时如何重新加载页面?

[英]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.

相关问题 如何单击链接但不重新加载页面? - How can I click a link but not reload the page? 当我点击指向子页面的链接时,如何在页面页脚中保留一个不重新加载的音乐播放器? - How can I keep a music player in the page footer that doesn't reload when I click a link to a subpage? 当我单击该行中的React组件链接时,如何防止该行单击? - How can I prevent a row click when I click a React component link within that row? 当我点击按钮时重新加载另一页 - Reload another page when I click on button 如何使用反应路由器链接组件重新加载页面? - How to reload a page using react router Link component? 单击链接后如何打开右侧的php页面? - How to open a php page on the right side when I click a link? 如何在按钮单击操作时重新加载同一页面,即在反应中删除? - How to reload the same page upon a button click action i.e delete in react? 当我单击浏览器中第一页中的按钮时,如何重新加载第二页? - How to reload 2nd page when i click the button in 1st page in browser? 从链接调用反应组件并传递道具时,页面重新加载失败 - Page reload fails when calling a react component from the Link and passing the props 在组件中单击链接时如何关闭 react-burger-menu? - How to close react-burger-menu when link click in a component?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM