How to use react router in Electron?


#1

We are building a desktop app using React+Electron. In React, you can use something like this to show different component (View) :www.abc.com#component2.

We don’t know how to make electron load a local file with #, something like this

It dosen’t work.

Please help. We have searched for a while, but nothing really helpful was found.


#2

I believe it’s because you’re including the hash fragment within the pathname, which gets encoded as /build/index.html%23page2, and that file/url doesn’t exist.

What you need to do is something like this:

const path = require('path')
const url = require('url')

url.format({
    pathname: path.join(__dirname, './foo/bar'),
    hash: 'baz',
    protocol: 'file',
    slashes: true,
})

Notice the hash property. :slightly_smiling_face: