How to use React Router with Electron?

Sometimes, we want to use React Router with Electron.

In this article, we’ll look at how to use React Router with Electron.

How to use React Router with Electron?

To use React Router with Electron, we can use HashRouter.

For instance, we write

import { HashRouter, Route } from "react-router-dom";

to import HashRouter.

Then we write

<HashRouter>
  <div>
    <Route path="/" exact component={Home} />
    <Route path="/firstPage" component={FirstPage} />
    <Route path="/secondPage" component={SecondPage} />
  </div>
</HashRouter>

to wrap HashRouter around our Routes to render them.

HashRouter works because Electron runs our React app in a file-based environment.

Conclusion

To use React Router with Electron, we can use HashRouter.