在页面刷新时将React重定向到主页

时间:2019-12-24 04:41:42

标签: reactjs react-redux react-router react-router-dom

当用户刷新当前页面时,react将其重定向到主页。我该如何预防?强制刷新页面的网址不应更改。我也尝试过HashRouter,但是没有运气。有人可以建议我我在做什么错吗?

我正在使用的版本

"react": "^16.11.0",
"react-dom": "^16.12.0",
"react-router-dom": "^5.1.2",

index.js

ReactDOM.render(<App />, document.getElementById('root')); 

App.js

import { BrowserRouter as Router } from 'react-router-dom';
import BaseRouter from './routes';
import CustomLayout from './containers/Layout';
import { Provider } from "react-redux";
import store from "./store";

class App extends Component {
  render() {
    return (
      <React.Fragment>
        <Provider store={store}>
          <Router>
            <CustomLayout {...this.props}>
              <BaseRouter />
            </CustomLayout>
          </Router>

        </Provider>
      </React.Fragment>
    );
  }
}

这是route.js的一部分

        <React.Fragment>
            {!token ? <Redirect to='/login/' /> :
                <Switch>
                    <Route exact path='/' component={HomePage} />
                    <Route exact path='/customers/' component={Customers} />
                    <Route exact path='/some-route/' component={someRoute} />
                    <Route component={Page404} />
                </Switch>
            }
        </React.Fragment>

0 个答案:

没有答案