score:2
You only need to have on BrowserRouter in your component so that all Routes are aware of which Router they are subscribing to and at the same time Links are updating data in the correct Router Provider.
In your case, Header and Root elements are wrapped by Different BrowserRouter
and hence the Links
in Header component update the enclosing Router
Provider, which the other Routes
in index.js file aren't subscribing to and hence it doesn't work correctly.
So instead of rendering different elements with ReactDOM.render
, you can do it simply like
import React from 'react';
import ReactDOM from 'react-dom';
import { BrowserRouter, Route, Switch } from 'react-router-dom';
import Home from './Home';
import Header from './Header';
import Footer from './Footer';
import Resume from './Resume';
import * as serviceWorker from './serviceWorker';
ReactDOM.render((
<BrowserRouter>
<div>
<div id="header">
<Route component={Header} />
</div>
<Switch>
<Route exact path ="/" component={Home} />
<Route path ="/resume" component={Resume} />
</Switch>
<div id="footer">
<Route component={Footer} />
</div>
</div>
</BrowserRouter>
), document.getElementById('root'));
serviceWorker.unregister();
And Header.js
import React, { Component } from 'react';
import bImg from './img/moi2.jpg';
import { BrowserRouter, Link } from 'react-router-dom';
/**
* Composant définissant l'entête de la page (Navigation et la photo de début)
* Link est utilisé à la place de <a> pour la gestion des routes.
* @Author
* @date 15th november 2018
*/
class Header extends Component{
render(){
return(
<div>
<nav className="navbar navbar-expand-lg navbar-light fixed-top" id="mainNav">
<div className="container">
<Link className="navbar-brand" to="/">Iam Root</Link>
<button className="navbar-toggler navbar-toggler-right" type="button" data-toggle="collapse"
data-target="#navbarResponsive" aria-controls="navbarResponsive" aria-expanded="false"
aria-label="Toggle navigation">
Menu
<i className="fas fa-bars"></i>
</button>
<div className="collapse navbar-collapse" id="navbarResponsive">
<ul className="navbar-nav ml-auto">
<li className="nav-item">
<Link to="/resume">Mon CV</Link>
</li>
<li className="nav-item">
<Link to="/tutoriels">Tutoriels</Link>
</li>
<li className="nav-item">
<Link to="/contact">Contactez-moi</Link>
</li>
</ul>
</div>
</div>
</nav>
<header className="masthead" style={{backgroundImage: `url(${bImg})`}}>
<div className="overlay"></div>
<div className="container">
<div className="row">
<div className="col-lg-8 col-md-10 mx-auto">
<div className="site-heading">
<h1 style={{ opacity: 0.4}}>prenom nom</h1>
<span className="subheading">
Fonction
</span>
</div>
</div>
</div>
</div>
</header>
</div>
);
}
}
export default Header;
Once you make this change, your App should work as desired
score:0
You are rendering header and the other part of the application separately because of that React Router actually isn't aware of the other part of your application so it isn't able to reload the page. What you should do is creating something like a layout component and put all your components there so that you can have single BrowserRouter hoc.
Source: stackoverflow.com
Related Query
- React js Router changes url but not loading page
- React router changes url but not page loading
- react router changes url but not navigating to page
- React router changes url but not loading view
- React router changes url but not view
- React Router v5: history.push() changes the address bar, but does not change the page
- URL changes but page new page is not rendered Ionic React , IonReactRouter with history
- React router changes url but not the view (only on Internet Explorer 11)
- React Router useHistory. History.push changes url but does not load component
- React Router changes URL but doesn't update the page unless it was refresh from browser
- React router is changing the URL but not loading the webpage properly
- React Routing: URL is changing but page is not loading
- React Navlink changes url but not page
- React Router Dom & Firebase Auth: history.push changes url but does not render new component when called after firebase auth create account
- URL changes but component is not render - React Router
- React router V5 updating URL but not refreshing page
- React router changes url but not context is not updated
- React router changes url but not the component [SOLVED]
- react router dom, replaces the browser url but does not go to the page
- Router not navigating to next page but url changes
- React Router Dom - React Router changing url but not page
- React Router - URL changes but not Component
- ReactJs Router changes url but not the page
- why react router url changes but component does not load in my nested router app
- URL changes with React Router but component not rendering?
- React router changes the url but not reload the component
- I am having an issue with react router as my url is getting updated on clicking on it but the page is not getting rendered
- React Routes are not working properly the URl changes but its rendering the one same page on any link i click ,
- React router url changes correctly but component not being rendered from button component link
- Url is Changing but Requested Page is not Loading in React js
More Query from same tag
- http-proxy-middleware econnreset error after successful post request
- How to add "CLEAR" button to IonDateTime?
- download sample file in public folder (react)
- Why does my component render twice when using React's Context API and the useEffect hook?
- Adjust page size in ReactToPrint
- converting timestamp to fromnow() , moment showing 51 years fix value
- Materialize Css Parallex is not a function
- (Gatsby) Easiest way to diverge from the `SEO.defaultProps` on a per-document basis?
- if not on route('/') go to that route after submitting form, react
- How to give a custom type to an array key that is obtained by Object.values in Typescript?
- CSS reset values on Media Query Breakpoint (Resize window or Orientation changes)
- Home does not contain an export named Home
- TypeError: Cannot read property 'setState' of null
- force react.js to load depenencies via ssl
- ReactJS: How can I get the owner of a component?
- How to handle select change using material-ui in reactjs and Mongodb
- ListItem as a submit button
- React testing onSubmit using axios
- logging unmount for JSX element instead of components
- React inline style with a variable from a function
- Using Props And State To Pass Value Of Select Box To Child Component React.JS
- Gatsby build production type undefined errors
- Redux componentDidUpdate get previous state
- Is it possible to pass different props from two different files in React?
- How would I test for a react router route param in react-testing-library?
- react redux Promise function to async/await
- Display multiline error in Materials-UI TextField
- React, componentWillReceive lifeCycle Hook not being invoked (on passing props)
- How to pass AppSettings from ASP.NET Core MVC Controller to ReactJS / Redux ClientApp?
- React + Redux - wait for action to end?