# (Routing Issue in React) React Router creates duplicate history to the same location?

**URL:** <https://forum.freecodecamp.org/t/routing-issue-in-react-react-router-creates-duplicate-history-to-the-same-location/423089>\
**Category:** General\
**Created:** [September 28, 2020, 2:43pm UTC](https://forum.freecodecamp.org/t/routing-issue-in-react-react-router-creates-duplicate-history-to-the-same-location/423089 "2020-09-28T14:43:48Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![miratorimoolight](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/miratorimoolight/32/203988_2.png) [@miratorimoolight](https://forum.freecodecamp.org/u/miratorimoolight)\
**Post date:** [September 28, 2020, 2:43pm UTC](https://forum.freecodecamp.org/t/routing-issue-in-react-react-router-creates-duplicate-history-to-the-same-location/423089/1 "2020-09-28T14:43:48Z")

</div>

When I’m on “/home” then I click “/about” multiple times, then when I click back, it doesn’t go back to “/home”. Instead, there are many duplicate “/about” that I need to click back.

I have checked the github issue of React Router ([here](https://github.com/ReactTraining/react-router/issues/5362#)), but couldn’t find a clear solution, and everything seems so confusing.

Can anyone help please?

---

<div class="post-metadata">

**Author:** ![skaparate](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/skaparate/32/125896_2.png) [@skaparate](https://forum.freecodecamp.org/u/skaparate)\
**Post date:** [September 28, 2020, 5:19pm UTC](https://forum.freecodecamp.org/t/routing-issue-in-react-react-router-creates-duplicate-history-to-the-same-location/423089/2 "2020-09-28T17:19:07Z")

</div>

Hello!

You can add the hook shown at the bottom of the github issue page to each of your components that need to be routed or to the `<Switch>` ([sample project](https://github.com/skaparate/react-duplicate-routing)):

### LocationBlocker.js

```auto
import { useEffect } from "react";
import { useHistory } from "react-router-dom";

export default function useLocationBlocker() {
  const history = useHistory();
  useEffect(
    () => {
      history.block(
        (location, action) =>
          action !== "PUSH" ||
          getLocationId(location) !== getLocationId(history.location)
      );
    },
    [] // eslint-disable-line react-hooks/exhaustive-deps
  );
}

function getLocationId({ pathname, search, hash }) {
  return pathname + (search ? "?" + search : "") + (hash ? "#" + hash : "");
}

```

### Routes.jsx

```auto
import Contact from './Contact';
import About from './About';
import useLocationBlocker from "./LocationBlocker";

export default function Routes() {
  useLocationBlocker();
  return (
    <Switch>
      <Route path="/about">
        <About />
      </Route>
      <Route path="/contact">
        <Contact />
      </Route>
      <Route path="/">
        <h2>Home</h2>
      </Route>
    </Switch>
  );
}

```

Then, inside your `BrowserRouter`:

```auto
<BrowserRouter>
  <nav>
    <Link to="/">Home</Link>
    <Link to="/about">About</Link>
  </nav>
  <Routes />
</BrowserRouter>

```

I’m not an expert, so there may be another, better/shorter, way of writing this code.

Anyway, I hope it helps 😛,

Regards!

---

<div class="post-metadata">

**Author:** ![miratorimoolight](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/miratorimoolight/32/203988_2.png) [@miratorimoolight](https://forum.freecodecamp.org/u/miratorimoolight)\
**Post date:** [September 29, 2020, 7:54am UTC](https://forum.freecodecamp.org/t/routing-issue-in-react-react-router-creates-duplicate-history-to-the-same-location/423089/3 "2020-09-29T07:54:21Z")

</div>

Thanks mate for your reply! But I cannot import `useHistory`.  
It always show this error: `'react-router-dom' does not contain an export named 'useHistory'.` I’ve searched on the internet and found that the react-router-dom version 6.0.0 does not have it. But my react-router-dom is just version 4.3.1 but still it don’t work. Maybe that is a bug from the library??

---

<div class="post-metadata">

**Author:** ![miratorimoolight](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/miratorimoolight/32/203988_2.png) [@miratorimoolight](https://forum.freecodecamp.org/u/miratorimoolight)\
**Post date:** [September 29, 2020, 2:25pm UTC](https://forum.freecodecamp.org/t/routing-issue-in-react-react-router-creates-duplicate-history-to-the-same-location/423089/4 "2020-09-29T14:25:19Z")

</div>

Final solution:

- Do as @skaparate told
- But:
  - react version must be \>= 16.8
  - react-router-dom must be in version 5 NOT 6 NOT 4

---

<div class="post-metadata">

**Author:** ![skaparate](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/skaparate/32/125896_2.png) [@skaparate](https://forum.freecodecamp.org/u/skaparate)\
**Post date:** [September 29, 2020, 2:53pm UTC](https://forum.freecodecamp.org/t/routing-issue-in-react-react-router-creates-duplicate-history-to-the-same-location/423089/5 "2020-09-29T14:53:29Z")

</div>

Oh, yeah! I didn’t take into account the library version; I just assumed it was the latest.

Anyway, glad you solved it 🙂.

---

<div class="post-metadata">

**Author:** ![miratorimoolight](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/miratorimoolight/32/203988_2.png) [@miratorimoolight](https://forum.freecodecamp.org/u/miratorimoolight)\
**Post date:** [September 30, 2020, 8:27am UTC](https://forum.freecodecamp.org/t/routing-issue-in-react-react-router-creates-duplicate-history-to-the-same-location/423089/6 "2020-09-30T08:27:38Z")

</div>

I have a small doubt @skaparate! Just to clarify, does **useEffect(…)** work like **componentDidMount()** which was only called once when the **Routes** component mounted.  
If I do **history.block(…)** without using **useEffect(…)**, is that fine? because I see it works as well.

---

<div class="post-metadata">

**Author:** ![skaparate](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/skaparate/32/125896_2.png) [@skaparate](https://forum.freecodecamp.org/u/skaparate)\
**Post date:** [September 30, 2020, 1:46pm UTC](https://forum.freecodecamp.org/t/routing-issue-in-react-react-router-creates-duplicate-history-to-the-same-location/423089/7 "2020-09-30T13:46:21Z")

</div>

Yes! In fact, if you wanted to use purely functional react, that’s [one way to achieve similar `componentDidMount` functionality](https://reactjs.org/docs/hooks-effect.html) you get with classes.

---

<div class="post-metadata">

**Author:** ![miratorimoolight](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/miratorimoolight/32/203988_2.png) [@miratorimoolight](https://forum.freecodecamp.org/u/miratorimoolight)\
**Post date:** [September 30, 2020, 2:27pm UTC](https://forum.freecodecamp.org/t/routing-issue-in-react-react-router-creates-duplicate-history-to-the-same-location/423089/8 "2020-09-30T14:27:12Z")

</div>

So do you think it is fine to do `history.block(...)` without `useEffect(...)`?

I tried it in `Routes` component (functional component), and when I do `console.log`, it only calls the function that creates `Routes` once . So maybe it calls `history.block(...)` only once too 😅 which means I may not need to `useEffect()`

---

<div class="post-metadata">

**Author:** ![skaparate](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/skaparate/32/125896_2.png) [@skaparate](https://forum.freecodecamp.org/u/skaparate)\
**Post date:** [September 30, 2020, 2:29pm UTC](https://forum.freecodecamp.org/t/routing-issue-in-react-react-router-creates-duplicate-history-to-the-same-location/423089/9 "2020-09-30T14:29:48Z")

</div>

Yes, it’s fine. ~~Actually, if you read the last comment on the issue you posted, the proposed solution is to [use `history` directly](https://github.com/ReactTraining/history/issues/470#issuecomment-363449663)~~

---

<div class="post-metadata">

**Author:** ![skaparate](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/skaparate/32/125896_2.png) [@skaparate](https://forum.freecodecamp.org/u/skaparate)\
**Post date:** [September 30, 2020, 2:33pm UTC](https://forum.freecodecamp.org/t/routing-issue-in-react-react-router-creates-duplicate-history-to-the-same-location/423089/10 "2020-09-30T14:33:56Z")

</div>

Scratch that. I was wrong. I was confused with the answers 😅.

You can use history directly as shown here:

```auto
function applyBrowserLocationBlocker(history: History) {
  let currentLocation = null

  history.block((location, action) => {
    const nextLocation = location.pathname + location.search

    if (action === 'PUSH') {
      if (currentLocation === nextLocation) {
        return false
      }
    }

    currentLocation = nextLocation
  })
}

```

The code of my previous answer uses this function as an `effect` in react.

---

<div class="post-metadata">

**Author:** ![miratorimoolight](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/miratorimoolight/32/203988_2.png) [@miratorimoolight](https://forum.freecodecamp.org/u/miratorimoolight)\
**Post date:** [September 30, 2020, 2:39pm UTC](https://forum.freecodecamp.org/t/routing-issue-in-react-react-router-creates-duplicate-history-to-the-same-location/423089/11 "2020-09-30T14:39:21Z")

</div>

Thank you very much, mate!! You’ve been very helpful. I’m very grateful 🥳 🥳. It’s nice to meet you.

---

<div class="post-metadata">

**Author:** ![ILM](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/ilm/32/363239_2.png) [@ILM](https://forum.freecodecamp.org/u/ILM)\
**Post date:** [June 1, 2021, 6:38pm UTC](https://forum.freecodecamp.org/t/routing-issue-in-react-react-router-creates-duplicate-history-to-the-same-location/423089/12 "2021-06-01T18:38:27Z")

</div>

Closed on June 1, 2021, 6:38pm UTC


