How to use React Router with Electron?

后端 未结 7 905
生来不讨喜
生来不讨喜 2020-12-14 00:08

Using this boilerplate as reference I created an Electron app. It uses webpack to bundle the scripts and express server to host it.

Webpack config is practically sam

相关标签:
7条回答
  • 2020-12-14 00:22

    What about simply using Switch to default to "/" as follows:

    <Switch>
      <Route path="/" exact component={Home}/>
      <Route path="/foo" component={Foo}/>
      <Route path="/bar" component={Bar}/>
      <Route render={() => <Redirect to="/"/>}/>
    </Switch>
    

    This way, "/index.html" will redirect to "/"

    0 讨论(0)
  • 2020-12-14 00:25

    Another option would be to use hashHistory instead. Actually, in your referenced repo you can see that they're using hashHistory, how about trying that and posting back?

    0 讨论(0)
  • 2020-12-14 00:25

    Best option at the time of this answer is to use the MemoryRouter, worked for me :)

    0 讨论(0)
  • 2020-12-14 00:28

    I'm using React Router v4 and didn't want to fallback to the HashRouter, so I solved it with something amongst the likes of:

    import { Redirect, BrowserRouter } from 'react-router-dom';
    
    const App = () => (
      <BrowserRouter>
        <div>
          {window.location.pathname.includes('index.html') && <Redirect to="/" />}
        </div>
      </BrowserRouter>
    );
    
    0 讨论(0)
  • 2020-12-14 00:32

    The (current) react-router docs say:

    Generally speaking, you should use a <BrowserRouter> if you have a server that responds to requests and a <HashRouter> if you are using a static file server.

    An Electron app is basically a static file server.

    MemoryRouter can also work, so long as all routing originates from within the React part of the app. It only falls down when you want to navigate to a specific page from the Browser process, e.g. you want to pop up a new window and navigate directly to a "General Preferences" page. In that case, you can do this with HashRouter:

    prefsWindow.loadURL(`file://${__dirname}/app/index.html#/general-prefs`);
    

    I don't think there is a way to do that with MemoryRouter (from the Browser process).

    0 讨论(0)
  • 2020-12-14 00:33

    Had to Replace BrowserRouter with HashRouter.

    import {
      HashRouter,
      Route
    } from "react-router-dom";
    

    And then in my index.js or the entry file of the Electron app I had something like this:

    <HashRouter>
      <div>
        <Route path="/" exact     component={ Home } />
        <Route path="/firstPage"  component={ FirstPage } />
        <Route path="/secondPage" component={ SecondPage } />
      </div>
    </HashRouter>
    

    And then everything just worked.

    The reasoning: BrowserRouter is meant for request-based environments whereas HashRouter is meant for file-based environments.

    Read more here:

    • https://github.com/ReactTraining/react-router/blob/master/packages/react-router-dom/docs/api/HashRouter.md
    0 讨论(0)
提交回复
热议问题