How to trick react router into thinking `/` is the mount directory?

旧时模样 提交于 2021-02-11 14:13:49

问题


Let's say I have a simple react app:

import React from 'react';
import ReactDOM from 'react-dom';
import {BrowserRouter as Router, Route} from 'react-router-dom'
import * as serviceWorker from './serviceWorker';

import App from './App';
import About from './components/pages/About'
import Header from './components/layout/Header';

ReactDOM.render(
  <React.StrictMode>
    <Router>
      <Route path = "/" component={Header} />
      <Route exact path="/" component= {App} />
      <Route path="/about" component = {About} />
    </Router>
  </React.StrictMode>,
  document.getElementById('root')
);

serviceWorker.unregister();

where Header is like

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

const Header = () => (
    <header style={headerStyle}>
        <h1>My Fancy App</h1>
        <div><Link to="/" style={headerLinkStyle}>Home</Link> | <Link to="/about" style={headerLinkStyle}>About</Link></div>
    </header>
)

const headerStyle = {
    display: 'flex',
    flexDirection: 'column',
    alignItems: 'center',

    padding: '.25em',

    background: '#333',
    color: 'white'
}

const headerLinkStyle = {
    color: 'white'
}

export default Header

This is working fine.

The problem, now, is when I host this on github pages.

Because of how github pages works, the page will be hosted on https://myGithubAccount.github.io/my-repo-name/

If I click the Home link, however, I won't be redirected to https://myGithubAccount.github.io/my-repo-name/, I will instead be redirected to https://myGithubAccount.github.io/

and similarly, the About link won't take me to https://myGithubAccount.github.io/my-repo-name/about but to https://myGithubAccount.github.io/about.

Now by some react witchery, that actually works -- so long as you don't try to reload the page -- but it's clearly suboptimal.

I can, of course, just change all my usages of / to /my-repo-name, but if I go down that rabbit hole, I will have to go back and change it when I later give it a proper URL and then again when I host it somewhere else, and then again when ...

I'd rather have react consider / to be wherever the application is hosted (which is how it should be for properly encapsulated web apps).

I.e. on localhost, / will be localhost:3000/ and on github pages, / will be https://myGithubAccount.github.io/my-repo-name/

How do I do that?


回答1:


Follow the below steps :

  1. Set the basename
<Router basename={'/directory-name'}>
  <Route path='/' component={Home} />
  {/* … */}
</Router>
  1. Set the app homepage(in package.json file)

"homepage": "https://myapp.com/directory-name",

  1. Update the Routes
Router basename={'/subdirectory'}>
  <Route path={`${process.env.PUBLIC_URL}/`} component={Home} />
  <Route path={`${process.env.PUBLIC_URL}/news`} component={News} />
  <Route path={`${process.env.PUBLIC_URL}/about`} component={About} />
</Router>
  1. Update the Links

<Link to={${process.env.PUBLIC_URL}/page-path}>…</Link>

For detailed information , have a look at this link https://medium.com/@svinkle/how-to-deploy-a-react-app-to-a-subdirectory-f694d46427c1



来源:https://stackoverflow.com/questions/62640531/how-to-trick-react-router-into-thinking-is-the-mount-directory

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!