简体   繁体   English

如何在不重新加载页面的情况下更改浏览器 URL?

[英]How to change the browser URL without reload the page?

I'm using react routers and to change the URL I'm doing history.push(pathname), But it is reloading entire page.我正在使用反应路由器并更改 URL 我正在执行 history.push(pathname),但它正在重新加载整个页面。 I don't want to refresh or reload the page, I tried with replace and replacestate still page is reloading could anyone suggest me the solution?我不想刷新或重新加载页面,我尝试使用 replace 和 replacestate 仍然页面正在重新加载,有人可以建议我解决方案吗?

 history.push(
        `/dashboard/filter?studyName=${JSON.stringify(study_dashboard_new)?JSON.stringify(study_dashboard_new):JSON.stringify([])}&compoundName=${JSON.stringify(compound_dashboard_new)?JSON.stringify(compound_dashboard_new):JSON.stringify([])}`
    );

Thanks谢谢

You should use history from the react-router-dom 's useHistory hook.您应该使用react-router-domuseHistory钩子中的history

import { useHistory } from "react-router-dom";

function HomeButton() {
  const history = useHistory();

  function handleClick() {
    history.push("/home");
  }

  return (
    <button type="button" onClick={handleClick}>
      Go home
    </button>
  );
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM