score:2
Accepted answer
The issue here is that Navbar component is using a Router on its own and hence the Link components are not able to communicate to the outer Router
component which renders the Routes. Make sure you use only one Router component instance.
You Navbar component doesn't need a Router
instance
class NavBar extends Component {
render() {
return (
<header className='navbar'>
<nav className='navbar_navigation'>
<div />
<div className='navbar_logo'>
<Link to='/'>Home</Link>
</div>
<div className='navbar_nav-items'>
<ul>
<li><Link to="/signup" className="nav-link">Sign up</Link></li>
<li><Link to="/login" className="nav-link">Log In</Link></li>
<li><Link to="/guest-env" className="nav-link">Continue as guest</Link></li>
</ul>
</div>
</nav>
</header>
);
}
}
score:-1
If you want to render just one component based on route, you need to use the Switch
component from react-router:
import React, { Component } from 'react';
import {BrowserRouter as Router, Route, Switch} from "react-router-dom"
class App extends Component {
render() {
return (
<Router>
<div className="App">
<NavBar />
<Switch>
<Route exact path="/">
<Page 1 />
</Route>
<Route exact path="/login">
<Page 2 />
</Route>
... etc
</Switch>
</div>
</Router>
);
}
}
export default App;
Source: stackoverflow.com
Related Query
- Reactjs - changing url using react router but the component does not change
- ReactJS - React Router not changing component but url is changing
- react router link changing url but not the component
- React router 4 `Link` component only changing the url and not updating the route
- React Router v5: history.push() changes the address bar, but does not change the page
- React router does not change view, but url is changed - App is load inside iframe
- React Router useHistory. History.push changes url but does not load component
- React router goes to proper URL but does not update the view
- React 18 router changing url but not component
- React reach router link tag updates url but not the component
- React router is changing the URL but not loading the webpage properly
- Using React Router the <Redirect /> Does not redirect anywhere, but why?
- React Router v6 Link component not changing the page at root url
- React Router Dom & Firebase Auth: history.push changes url but does not render new component when called after firebase auth create account
- React router receives match params but does not update the component
- React router Link is changing the url but not actually redirecting
- React router 4 - Why is my component only rendered on manually setting the url but not on clicking a Link with same url?
- React router changes url but not the component [SOLVED]
- React Router Link is changing URL but the component remains same
- react router dom, replaces the browser url but does not go to the page
- React Router url updates but component does not
- React Router changes route but does not render the component
- Multiple params with React Router change url but not component
- In react Router i cant change the view but only the url is changing
- why react router url changes but component does not load in my nested router app
- My React Router Links are not working. It's showing the url change in the browser but contents are not showing
- React Router - The Address Bar Is Changing But Not Rendering The Component
- React router changes the url but not reload the component
- React guard change the route but does not show the component
- React Router Link Not Rendering the Component but changes the URL
More Query from same tag
- What does 'contravariance' on AbstractRecoilValue<T> mean in Recoil source type declarations?
- process is not defined on hot reload
- React component mounting/unmounting with regard to if else statement in render
- if/else statement to return react component
- Why is React Router taking me to the correct path, but not displaying the corresponding component?
- InvalidOperationException: The NPM script 'start' exited without indicating that the create-react-app server was listening for requests
- React - parent class component function change class of child component
- What is the difference between class componentName extends Component {} and export class componentName extends Component {}
- Meteorjs React “Uncaught Error: Cannot find module ‘/imports/ui/app’”
- passing data and mapping to my List component for display purpose but showing "undefined" text
- How to add polyfill for String.repeat function for ie11?
- React App crashes when fetching searchresults from django API
- font doesn't apply in ReactJS with Bootswatch
- Why is filter() returning an error in my React reduxjs/toolkit slice?
- Error: Invariant failed: You should not use <Redirect> outside a <Router> ---- ReactJS form with Yup+Formik+Hooks
- fetching problems with react useEffect
- Where to put utility functions in a React-Redux application?
- Dynamically create a ListItem comprised of N ListItemTexts
- React: Stop click event propagation when using mixed React and DOM events
- Delay load on every page with reach-router
- MUI Stepper Icon not showing
- Migrate to new component on clicking submit button in react
- react-hook -form with SliderComponent input
- React CSS transitions doesn't work
- How can I UpdateUserProfile when signing up user, in Firebase-v9, with Typescript?
- Keep inactive nav-tab displayed when input inside of it has focus
- javascript/react fetch returning a promise instead of result
- How to use a useState in react-hooks with percentage and conditions?
- Dynamically requiring images for React/Webpack OR what is cost of many require(img)
- Update table data in switch in mui-datatables while my data is in boolean