Axzen
Axzen

Reputation: 11

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.

I set a route for index.html to send the user to the home page, but that is a temporary fix. 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.

Upvotes: 0

Views: 875

Answers (1)

nir99
nir99

Reputation: 86

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.

Upvotes: 1

Related Questions