0

I have an electron app that uses React.

I use react-router-dom to route different windows to different components -

ReactDOM.createRoot(document.getElementById('root') as HTMLElement).render(
    <React.StrictMode>
        <HashRouter>
            <Routes>
                <Route path='/' element={<App />} />
                <Route path='add' element={<LinkDialog/>} />
                <Route path='modify' element={<LinkDialog/>} />
                <Route path='edit' element={<EditDialog />} />
            </Routes>
        </HashRouter>
    </React.StrictMode>
)

To load each route on each window I use something like this:

const VITE_DEV_SERVER_URL = process.env['VITE_DEV_SERVER_URL']
process.env.DIST = path.join(__dirname, '../dist')
const loadRoute = (window, route) => {
    if (VITE_DEV_SERVER_URL) {
        const url = new URL(VITE_DEV_SERVER_URL)
        url.pathname = route
        window.loadURL(url.href)
    } else {
        window.loadFile(path.join(process.env.DIST, '../renderer/index.html'), { hash: route })
    }
}

When I'm debugging my code I use BrowserRouter and the pages show properly, but when build the app (And of course switch to HashRouter the pages won't render, and I can't figure out what I'm doing wrong.

Also, how can I switch the Router type automatically so I won't have to do it when changing between build/dev?

LITzman
  • 730
  • 1
  • 16

1 Answers1

1

After a few days of struggle I found electron-router-dom. It's a wrapper that leaves no room for error both in the electron process and in the renderer process. I used It, and my problem was solved

LITzman
  • 730
  • 1
  • 16