How to use react-router 4.0 to refresh current route? not reload the whole page

后端 未结 7 2182
暖寄归人
暖寄归人 2021-01-01 19:35

\'react-router-dom\' has refresh function here, but I don\'t know how to call this method, also their well formatted document doesn\'t bother to explain this.

windo

7条回答
  •  陌清茗
    陌清茗 (楼主)
    2021-01-01 20:18

    Only this worked for me:

    reloadOrNavigate = () => {
      const { history, location } = this.props;
      if (location.pathname === '/dashboard') {
        history.replace(`/reload`);
        setTimeout(() => {
          history.replace(`/dashboard`);
        });
      } else {
        history.push('/dashboard');
      }
    };
    

    The answer is similar to the previous one but I needed to add setTimeout function in order to make it work. In my case, I had to refresh the current URL by clicking the logo if I'm on the /dashboard page. First, it goes to extra route /reload (the name is up to you) and then immediately returns. The page becomes white for less than a second and appears with reloaded data. No reloading in browser occurs, it is still SPA which is good.

提交回复
热议问题