Introduction
A quick introduction to Electron Router DOM
If you've ever tried using the react-router-dom
library with Electron
,
you've probably had trouble getting it to work properly,
both in development and production environments.
From this, the Electron Router DOM
library was born,
which aims to facilitate the integration of react-router-dom
with Electron
and window routing,
where each window can have its own routing.
Features
- 🚀 Ready for production and development environments
- 📱 Window routing support
- 🌐 Support for
query strings
sent from the main process to the renderer - 🧬 Type-safe API designed to provide good DX
Installation
In your terminal and in the root folder of your application, run:
Creating your first routing
Create the electron-router-dom.ts
file
In your project's src
folder, create a lib
folder and within it the electron-router-dom.ts
(or .js
) file.
It is through this file that you will expose the registerRoute
method and the Router
component to your application.
- The registerRoute method will be used in the
main process
to register a window as an application route. - The Router component will be used in the
renderer process
to navigate between the application's windows/routes.
Update the main process
Import the registerRoute
method from the electron-router-dom.ts
file you created earlier:
And in the function where you create your application window, after creation, register the route by passing your window to registerRoute
:
Note that you no longer need to worry about the logic of loading the development server URL or the application HTML file, the Electron Router DOM
will take care of that for you.
Update the renderer process
Import the Router
component from the electron-router-dom.ts
file you created earlier:
And with that you will need to pass your routes to the Router
component, see an example:
🎉 You are now ready to use Electron Router DOM
To get the most out of the library, we recommend you to read the rest of the documentation or look at the existing examples!