Home > Back-end >  After the page is reloaded, the url created with useHistory push is deleted
After the page is reloaded, the url created with useHistory push is deleted

Time:02-13

I have such a code, but the url is reset after the page is refreshed. Is there any solution to this? I want the page to reload after the url changes to " localhost:3000/?chain=ETH ", but after the reload the url is reset. so only "localhost:3000" remains. How can I prevent this?

  const handleClickEthereum = async () => {
    try {
      await handleNetworkSwitch("ethereum");
      navigate({
        pathname: "/",
        search: "?chain=ETH",
      });
      window.location.reload(false);
      setVisible(false);
    } catch (error) {
      console.log(error);
    }
  };

CodePudding user response:

Try replacing navigate({ pathname: "/", search: "?chain=ETH", });

with: window.location.replace('/?chain=ETH');

CodePudding user response:

Hmm, can you share a little more code? How did you use the router etc? Which version of the package did you use? I put the same code in the random react app from my local machine and works perfectly, so looks that something else is wrong.

CodePudding user response:

This code is navigating to http://localhost:3000/?chain=ETH and then reload the page. And url also not reset to http://localhost:3000

export default function Home() {
  const [visible, setVisible] = useState(true);
  const navigate = useNavigate();

  const handleClickEthereum = () => (e) => {
    navigate({
      pathname: "/",
      search: "?chain=ETH"
    });
    window.location.reload();
    setVisible(false);
  };
  return (
    <div>
      {JSON.stringify(visible)}
      <button onClick={handleClickEthereum()}>Click ETH</button>
    </div>
  );
}
  • Related