I have setup the React with react-router
version 4. The routing works when I enter the URL directly on the browser, however when I click on the link, the URL changes on the browser (e.g http://localhost:8080/categories), but the content don't get updated (But if I do a refresh, it gets updated).
Below is my setup:
The Routes.js setup as follows:
import { Switch, Route } from 'react-router-dom'; import React from 'react'; // Components import Categories from './containers/videos/Categories'; import Videos from './containers/videos/Videos'; import Home from './components/Home'; const routes = () => ( <Switch> <Route exact path="/" component={Home}/> <Route path="/videos" component={Videos}/> <Route path="/categories" component={Categories}/> </Switch> ); export default routes;
The link I use in Nav.js are as follows:
<Link to="/videos">Videos</Link> <Link to="/categories">Categories</Link>
The App.js is as follows:
import React from 'react'; import './app.scss'; import Routes from './../routes'; import Nav from './Nav'; class AppComponent extends React.Component { render() { return ( <div className="index"> <Nav /> <div className="container"> <Routes /> </div> </div> ); } } AppComponent.defaultProps = { }; export default AppComponent;
react-router-dom allows us to navigate through different pages on our app with/without refreshing the entire component. By default, BrowserRouter in react-router-dom will not refresh the entire page.
Lazy Loading on route level with React Router is a powerful feature. Usually a client-side rendered React applications comes as one bundle from a web server. However, when enabling lazy loading, the bundle is split into smaller bundles.
The main difference between the two is the way they store the URL and communicate with your web server. A <BrowserRouter> uses regular URL paths. These are generally the best-looking URLs, but they require your server to be configured correctly.
Okay so here is the reason why react router urls could work while navigating within the website but not directly loading from browser – The routes in react-router-dom are virtual. They don’t exist as directory or page on server. They are just different components loaded for different navigation.
- This is because React isn’t meant to work that way. If you want to allow users to link to other pages on click, then you should consider the React Router to allow you route through different components. Why isn’t the link button executing the function I put in the onclick attribute?
Any request that is made to your server will respond with the index page (and then fetch any JS resources you need), React Router will then take over and load the appropriate view. The actual code for this varies on which type of server you have.
The reason for the dreaded Cannot GET /* error is because, if you’re at /dashboard and then hit refresh, the browser will make a GET request to /dashboard which will fail since you have no logic on your server for handling that request (since React Router is supposed to do it). In case the issue is still fuzzy, here’s another example.
I would go through your components and make sure you have only one <Router> ... </Router>
. Also -- make sure you have a <Router>...</Router>
There may be cases when you'd use more than one, but if you accidentally have nested routers (because you were hacking quickly and forgot to remove one when you were moving it around to all kinds of places ;-) - it could cause an issue.
I would try
import { BrowserRouter as Router, } from 'react-router-dom' // Other Imports ... return ( <Router> <div className="index"> <Nav /> <!-- In this component you have <Links> --> <div className="container"> <Routes /> </div> </div> </Router> );
In your top most component (App.js).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With