Skip to content

Instantly share code, notes, and snippets.

@thejohnhoffer
Last active December 22, 2021 09:52
Show Gist options
  • Save thejohnhoffer/5cb3798684c815683195847c53cff4ab to your computer and use it in GitHub Desktop.
Save thejohnhoffer/5cb3798684c815683195847c53cff4ab to your computer and use it in GitHub Desktop.

Return to the template

This replaces a feature that react-router-dom@6 lost in the upgrade from history@4 to history@5. Basically, this restores the ability to have a window.location.hash of #something instead of #/something.

In response to a core React Router dev's suggestion, @jtojnar showed that basename="" doesn't match the old hashType="noslash" feature. I asked for that option as a new feature, and I implemented it. This was closed, so I tried a sencod implementation, which also was not accpeted.

My third attempt involved two repositories, history and React Router. I added a hashRoot parameter to replicate the old hashType behavior in history. A react router maintainer dismissed my changes, but they haven't yet commented on the history pull request. The same maintainer originally hesitantly suggested we try to solve this at history.

With react-router-dom@6.1, the maintainers released a HistoryRouter class. The primary maintainer expresssed reservations, so the 6.1.1 release reclassified "HistoryRouter" as "unstable_HistoryRouter"! Oh well, it's still usable.

Now, my template is able to replcate the lost hashType="noslash" of react-router@5 in react-router@6 with some wonky imports:

import { useHashHistory } from "use-hash-history";
import { unstable_HistoryRouter as HistoryRouter } from "react-router-dom";

Eventually, if a new "HistoryRouter" replaces "unstable_HistoryRouter", the impports might look like this:

import { useHashHistory } from "use-hash-history";
import { HistoryRouter } from "react-router-dom";

At any rate, I plan to keep maintaining this project as React Router is updated. I worked on the v1.1.0 release to allow history PR #911 as a drop-in replacement for hashRoot if that feature were ever merged into history.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment