question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

react-router-dom v5 does not work with electron in production

See original GitHub issue

I’m having an issue using react-router-dom with electron. In development, the router correctly functions and routes users between the different pages. In production, the router no longer functions correctly. In addition to the repo showing the issue, I have some gifs that can quickly outline the problem. Working in development, not working in production.

I am not using redux, and I am using the HashRouter, but I have also tried the MemoryRouter with no luck…

edit I just tested with version 4 of react router dom and it works correctly in production and development. So it seems that this issue is related to version 5.

Version

"electron": "^5.0.0",
"electron-builder": "^20.39.0",
"electron-webpack": "^2.6.2",
"react-router-dom": "^5.0.0"

Test Case

https://github.com/kyle-mccarthy/react-router-dom-electron-issue

Steps to reproduce

The best way to reproduce this error is through the provided github repo.

Expected Behavior

The router should continue to work under the production environment.

Actual Behavior

The router does not correctly work in production.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:32
  • Comments:28 (2 by maintainers)

github_iconTop GitHub Comments

72reactions
granabercommented, Jan 22, 2020

Hi Hackers,

The problem with electron and react-router-dom is that electron when working in production mode (not really) is that you with electron are creating an application, (although it is known that it is a scan with js code running in backgroud), electron does not handle history and works with the synchronized URL, BrowseHistory does not work with electron, therefore you must use HashRouter that if you synchronize your URL with a UI that is (windows.location.hash).

This would be your final code for electron and react-router-dom to work perfectly

// Not BrowserHistory, this for pageweb
import { HashRouter, Route } from "react-router-dom";

class App extends Component {
  render() {
    return (
        <HashRouter> 
          <Route exact={true} 
                  path='/'    
                  render={(props) => 
                          <iLogin {...props}/>
                        }
          />
          <Route path='/InitMenu' component={InitMenu}/>
        </HashRouter>
    );
  }
}

This is the solution … it is not a problem of versions

Happy Hacking

8reactions
dwjohnstoncommented, May 3, 2019

Ok, got this sorted:

The issue was that in some places I was importing (IDE did this for me) Link like:

import { Link } from 'react-router-dom/cjs/react-router-dom';

changing it to:

import { Link } from 'react-router-dom';

solves the issue for me.

Read more comments on GitHub >

github_iconTop Results From Across the Web

react-router-dom v5 does not work with electron in production
I'm having an issue using react-router-dom with electron. In development, the router correctly functions and routes users between the ...
Read more >
How to use React Router with Electron? - Stack Overflow
Your solution works except reloading electron will lead to a blank page. It's not a problem in "production" but in development I often...
Read more >
react module not found: error: can't resolve 'fs' in - You.com
Your quick fix is to take react scripts down to v4 until a fix for v5 is in place unless you are comfortable...
Read more >
Fixing the 'cannot GET /URL' error on refresh with React ...
In this post you'll learn how to fix the 'cannot GET /URL' error with React Router. Along the way, you'll also learn how...
Read more >
errorElement v6.6.1 - React Router
When a route does not have an errorElement , errors will bubble up through parent ... at all about production bugs, which poses...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found