Two things that would certainly cause this,
<Switch>
<Route path="/" component={RootComponent} />
<Route path="/test" component={TestComponent} />
</Switch>
- Check your routes, do you have the base route without
exact
keyword?
The above will match all routes to the root route and will land you in the root component. You need to add exact
key
<Route exact path="/" component={RootComponent} />
- Do you have
homepage: "."
in package.json
or <base href="/">
set in index.html
? If yes, remove them.
Homepage: '.'
will default to serving from the root.
What are the props you have used in the MemoryRouter? Like the
initialIndex
, initialEntries
etc?
Did you try Hot module replacement
? I'm not suggesting this as a fix for this issue but it's a good workaround Electron-forge doc for HMR
entryPoints: [{
rhmr: 'react-hot-loader/patch', // react hot module replacement
name: 'main_window',
html: './src/renderer/index.html',
js: './src/renderer/index.js'
}]
Electron-forge doc says it's not possible to do HMR inside the renderer, can you try the above anyways? It worked for me in the webpack app.
Please provide a minimal reproducible code repo/codesandbox to help you with the fix if the above didn't help. Most importantly, need to see the MemoryRouter
usage in your app.
Update in response to the comment:
Yes, electron forge does hot reload by default, only changes to the CSS files are hot reloaded (without refresh), changes to JS files will need a refresh, in both cases files are being watched for changes. You can see note in the webpack-dev-server
config (snapshot below)
Looking at your gist on MemoryRouter
, I suggest below changes
Provide the memory history prop to the router.
import {createMemoryHistory} from 'history';
const history = createMemoryHistory();
<Router history={history}>
<Switch>
<Route path='/dash' exact component={Dash} />
<Route path='/wallet' exact component={Wallet} />
<Route path='/service' exact component={ServiceStatus} />
<Route path='/' exact component={SignIn} />
<Route component={SignIn} /> // would suggest protected routes
</Switch>
</Router>
Remove exact
from other routes than the base route and place base
above sign-in.
If the issue persists, I suggest you enable the hot module
replacement (which would update your app without reload)
Here is an elaborate writeup on Hot Module Replacement applauded by the co-creator of redux and create-react-app.
A gif on HMR taken from above post (No refresh, so no landing on root route on every change yet the app is updated):
An example app to see it in action.
Electron forge HMR solution
Electron forge react example project