[英]ReactJS - Refreshing the page always changes URL to index.html
I'm experiencing an issue when anyone refreshes the page, where the URL will always be set to index.html.当有人刷新页面时,我遇到了一个问题,其中 URL 将始终设置为 index.html。
I set a route for index.html to send the user to the home page, but that is a temporary fix.我为 index.html 设置了一个路由来将用户发送到主页,但这是一个临时修复。 When you refresh a page, it should just bring you back to that page.
当您刷新页面时,它应该只是将您带回该页面。
Why isn't it bringing the user back to the page and how can I set up the router so that it does?为什么它不能将用户带回页面,我该如何设置路由器才能做到这一点?
Thank you.谢谢你。
import React, { Component } from 'react';
import { BrowserRouter as Router, Route, Switch } from 'react-router-dom';
import './App.css';
import User from './User';
import Home from './Home';
class App extends Component {
render() {
return (
<div className="App">
<Router>
<div>
<Switch>
<Route exact path="/" component={Home} />
<Route exact path="/user" component={User} />
</Switch>
</div>
</Router>
</div>
);
}
}
export default App;
this way when you navigate to user page it will stay on the user page even when you refreshed.这样,当您导航到用户页面时,即使您刷新,它也会保留在用户页面上。 and make sure that you are using react-router-dom
https://github.com/ReactTraining/react-router/tree/master/packages/react-router-dom
refer this to learn more about it.并确保您使用的是 react-router-dom
https://github.com/ReactTraining/react-router/tree/master/packages/react-router-dom
参考此以了解更多信息。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.