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:
npm i electron-router-dom
Creating your first routing
⚠️ Important!
Electron Router DOM already manages the loadURL
and loadFile
methods of the BrowserWindow
from Electron, to avoid issues, do not use them manually!
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.
import { createElectronRouter } from 'electron-router-dom'
export const { Router, registerRoute } = createElectronRouter({
port: 4927, // the port of your React server is running on (optional, default port is 3000)
types: {
/**
* The ids of the windows of your application, think of these ids as the basenames of the routes
* this new way will allow your editor's intelisense to help you know which ids are available to use
* both in the main and renderer process
*/
ids: ['main'],
},
})
Update the main process
Import the registerRoute
method from the electron-router-dom.ts
file you created earlier:
import { registerRoute } from '../lib/electron-router-dom'
And in the function where you create your application window, after creation, register the route by passing your window to registerRoute
:
registerRoute({
id: 'main',
browserWindow: window,
htmlFile: path.join(__dirname, '../renderer/index.html'),
})
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:
import { Router } from '../lib/electron-router-dom'
And with that you will need to pass your routes to the Router
component, see an example:
import { Router } from 'electron-router-dom'
import { Route } from 'react-router-dom'
import { MainScreen, AboutScreen, SearchScreen } from './screens'
export function AppRoutes() {
return (
<Router
main={
<>
<Route path="/" element={<MainScreen />} />
<Route path="/search" element={<SearchScreen />} />
</>
}
about={<Route path="/" element={<AboutScreen />} />}
/>
)
}
🎉 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!