React-Router DOM not rendering the content from other pages - reactjs

// App.js
import React from 'react';
import Contact from './Contact';
import About from './About';
import Nav from './Nav';
import {BrowserRouter as Router, Switch, Route} from 'react-router-dom';
function Web() {
return (
<Router>
<Switch>
<Nav />
<Route exact path="/" Component={Home} />
<Route exact path="/contact" Component={Contact} />
</Switch>
</Router>
);
}
const Home = () => (
<div>
<h1>Home page</h1>
</div>
);
export default Web;
// Nav.js
import React from 'react';
import {Link} from 'react-router-dom';
class Nav extends React.Component {
render() {
return (
<nav class="navbar navbar-light bg-light justify-content-between">
<Link to="/contact">
<a class="navbar-brand">Contact</a>
</Link>
<Link to="/about">
<a class="navbar-brand">about</a>
</Link>
</nav>
);
}
}
export default Nav;
//Contact.js
import React from 'react';
class Contact extends React.Component {
render() {
return <h1>CONTACT PAGE</h1>;
}
}
export default Contact;
When I don't use the react-router it works fine and renders the contact page. When it comes to react router This renders only the NavBar page .When I click the Contact link in the Navbar the url changes but it's an empty page.I might have done some mistake but could not find out.

All children of a Switch component should be either Route or Redirect components.
Also, you are never passing the component prop to the route components because you have the prop name(as "Component") uppercased.
function Web() {
return (
<Router>
<Nav /> /* Nav should be outside of the switch */
<Switch>
<Route exact path='/' component={Home} /> /* lowercased prop name */
<Route exact path='/contact' component={Contact} />
</Switch>
</Router>
)
}

Name of props for <Route /> component should be camel cased with starting lower case letter.
You have:
<Route exact path='/' Component={Home} />
Try this:
<Route exact path='/' component={Home} />

Move your Nav component out of switch. i.e
// App.js
import React from 'react';
import Contact from './Contact';
import About from './About';
import Nav from './Nav';
import {BrowserRouter as Router, Switch, Route} from 'react-router-dom';
function Web() {
return (
<Router>
<Nav />
<Switch>
<Route exact path="/" Component={Home} />
<Route exact path="/contact" Component={Contact} />
</Switch>
</Router>
);
}
const Home = () => (
<div>
<h1>Home page</h1>
</div>
);
export default Web;
// Nav.js
import React from 'react';
import {Link} from 'react-router-dom';
class Nav extends React.Component {
render() {
return (
<nav class="navbar navbar-light bg-light justify-content-between">
<Link to="/contact">
<a class="navbar-brand">Contact</a>
</Link>
<Link to="/about">
<a class="navbar-brand">about</a>
</Link>
</nav>
);
}
}
export default Nav;
//Contact.js
import React from 'react';
class Contact extends React.Component {
render() {
return <h1>CONTACT PAGE</h1>;
}
}
export default Contact;

Related

React Router need help routing

I'm a beginner front-end dev and I try to make an imaginary website. I'm learning to react router now. So when I try to click on the home/about or contact button, It doesn't open it. What am I doing wrong?
here's my code below :
my App code :
import React from 'react';
import './App.css';
import Navibar from './Components/Navibar';
import {BrowserRouter as Router, Route, Switch} from 'react-router-dom';
import Home from './Components/Home';
import About from './Components/About';
import Contact from './Components/Contact';
let App = () => {
return (
<Router>
<div className="App">
<Navibar/>
<div className="content">
<Switch>
<Route exact path="/">
<Home/>
</Route>
<Route exact path="/About">
<About/>
</Route>
<Route exact path="Contact">
<Contact/>
</Route>
</Switch>
</div>
</div>
</Router>
);
}
export default App;
and here's my navbar code :
import React from 'react';
import {Nav, Navbar, Container} from 'react-bootstrap';
import {Link} from 'react-router-dom';
let Navibar = () => {
return (
<>
<Navbar bg="dark" variant="dark">
<Container>
<Navbar.Brand href="#home">My Portfolio</Navbar.Brand>
<Nav className="me-auto">
<Link to="/">Home</Link>
<Link to="#about">About me</Link>
<Link to="#contact">Contact</Link>
</Nav>
</Container>
</Navbar>
</>
);
}
export default Navibar;
and here's my Home (the home/about and contact codes are the same) code :
import React from 'react';
let Home = () => {
return (
<>
<h2>Home</h2>
</>
);
}
export default Home;
I need help from a professional, because I'm getting stuck in this :)
The Navibar should link to the route paths you are rendering, not anchor tags (hashes) on the current page. The bootstrap Navbar.Brand component should also render a RRD Link component instead of a plain anchor tag with href attribute.
import React from 'react';
import { Nav, Navbar, Container} from 'react-bootstrap';
import { Link } from 'react-router-dom';
const Navibar = () => {
return (
<>
<Navbar bg="dark" variant="dark">
<Container>
<Navbar.Brand
as={Link} // <-- as a RRD Link component
to="/" // <-- to prop for Link component
>
My Portfolio
</Navbar.Brand>
<Nav className="me-auto">
<Link to="/">Home</Link> // <-- "/"
<Link to="/about">About me</Link> // <-- "/about"
<Link to="/contact">Contact</Link> // <-- "/contact"
</Nav>
</Container>
</Navbar>
</>
);
}
export default Navibar;
The App component should specify route paths that match what the navbar is linking to.
Also the routes App is rendering within the Switch component should be rendered in inverse order of route path specificity. In other words, render the more specific paths prior to the less specific paths. This is to attempt to match and render more specific paths and falling back to less specific paths. This also effectively eliminates the need to use the exact prop.
const App = () => {
return (
<Router>
<div className="App">
<Navibar />
<div className="content">
<Switch>
<Route path="/about">
<About />
</Route>
<Route path="/contact">
<Contact />
</Route>
<Route path="/">
<Home />
</Route>
</Switch>
</div>
</div>
</Router>
);
}

React dom route

Here is my App.js code
import './App.css';
import Home from './Pages/Home';
import About from './Pages/About';
import Contact from './Pages/Contact';
import {BrowserRouter,Routes,Route ,Link} from 'react-router-dom';
function App() {
return (
<div>
<BrowserRouter>
<Routes>
<Route path="/" element={<Home/>}/>
<Route Path="/About" element={<About/>}/>
<Route Path="/Contact" element={<Contact/>}/>
</Routes>
</BrowserRouter>
</div>
);
}
export default App;
and Here is my About page
import React from 'react';
import {Link} from 'react-router-dom';
function About() {
return (
<div>
<h1>This is About</h1>
<Link to="About">Go to About</Link>
</div>
)
}
export default About
React Route is not working.I installed react router Dom already.please help me to fix
Not navigation working
Can you change this
<Link to="About">Go to About</Link>
to
<Link to="/About">Go to About</Link>
I have added / before the "About" in to.

This is a question about setting up routing routes

import React from 'react';
import {Link, Route, BrowserRouter as Router, Switch} from "react-router-dom";
import './App.css';
import User from './Component/User';
import AdContent from './Component/AdContent';
//Home.js
function Home(){
return(
<Router>
<header>
<div>ShareAdsLink</div>
<nav>
<Link to="/User">
<li className="button">LogIn</li>
</Link>
<Link to="/AdContent">
<li className="button">SignUp</li>
</Link>
</nav>
</header>
<Switch>
<Route path="/User" component={User} />
<Route path="/Adcontent" component={AdContent}/>
</Switch>
</Router>
);
}export default Home;
//App.js
import React from 'react';
import Home from './Home';
import {Route, BrowserRouter as Router, Switch} from "react-router-dom";
function App() {
return (
<Router>
<Route path="/" component={Home} exact/>
</Router>
);
}
export default App;
What I'm trying to do is to show the default screen (the page you see as soon as you enter) in Home.
There are login and signup buttons in Home, and if i click this button, a new page should appear. But in my results, a new page should be opened, separate from the contents of Home, but the contents of Home will be displayed too.
How can I display a new page? Using exact keywords is useless.
That is my first question.
Secondly, can you give me advice on where(ex, App.js ,,) to configure the base screen?
Restructuring your app like this should help:
//App.js
import React from "react";
import Home from "./Home";
import { Route, BrowserRouter as Router, Switch } from "react-router-dom";
import User from "./Component/User";
import AdContent from "./Component/AdContent";
function App() {
return (
<Router>
<Route path="/" exact component={Home} />
<Switch>
<Route exact path="/User" component={User} />
<Route path="/Adcontent" component={AdContent} />
</Switch>
</Router>
);
}
export default App;
//Home.jsx
import React from "react";
import { Link } from "react-router-dom";
function Home() {
return (
<header>
<div>ShareAdsLink</div>
<nav>
<Link to="/User">
<li className="button">LogIn</li>
</Link>
<Link to="/AdContent">
<li className="button">SignUp</li>
</Link>
</nav>
</header>
);
}
export default Home;
Issue
You are rendering your homepage header no matter what, regardless of path/page.
Solution
Just render the header content into its own route within the Switch, and move the routes into App. By placing the less specific home path ("/") last/later then more specific paths will be attempted to be matched first, and if no prior path is matched then the header will render.
function App() {
return (
<Router>
<Switch>
<Route path="/User" component={User} />
<Route path="/Adcontent" component={AdContent} />
<Route path="/">
<header>
<div>ShareAdsLink</div>
<nav>
<Link to="/User">
<li className="button">LogIn</li>
</Link>
<Link to="/AdContent">
<li className="button">SignUp</li>
</Link>
</nav>
</header>
</Route>
</Switch>
</Router>
);
}

Routes are not responding with Back and forward browser button when using nested routes in reactjs

Hi guys I am a newbie to reactjs, I am trying to learn reactjs router. I have this weird thing happening to my Dashboard routes.
My App component have two routes "/login" and "/"
'/login' shows up the login screen and the '/' shows up the main dashboard screen.
In the dashboard I have sidenavigation component and Main content component.
I am trying to nest a set of Routes inside main component.
The routes are working fine when i navigate it through the links in the side navigation component. But when I try to click back or forward button in the browser it doesn't work. I am getting a blank screen without anything loaded.
please need your help guys.. any help will be highly appreciated.
Please find the code below.
//App.js
import React, { Component } from "react";
import { BrowserRouter, Route, Switch } from "react-router-dom";
import MainPage from "./Mainpage";
import LoginPage from "./components/pages/LoginPage";
import "./styleFiles/index.css";
class App extends Component {
render() {
return (
<div className="flexible-content">
<Route path="/" exact component={MainPage} />
<Route path="/login" component={LoginPage} />
</div>
);
}
}
export default App;
//MainPage.js
import React, { Component } from "react";
import { BrowserRouter, Route, Switch } from "react-router-dom";
import SideNavigation from "./components/sideNavigation";
import Footer from "./components/Footer";
import "./styleFiles/index.css";
import DashboardPage from "./components/pages/DashboardPage";
import ProfilePage from "./components/pages/ProfilePage";
import TablesPage from "./components/pages/TablesPage";
import InventoryPage from "./components/pages/InventoryPage";
class MainPage extends Component {
render() {
return (
<BrowserRouter>
<div className="flexible-content">
<SideNavigation />
<main id="content" className="p-5">
<Switch>
<Route path={"/"} exact component={DashboardPage} />
<Route path={"/Dashboard"} exact component={DashboardPage} />
<Route path="/profile" component={ProfilePage} />
<Route path="/tables" component={TablesPage} />
<Route path="/inventory" component={InventoryPage} />
<Route component={DashboardPage} />
</Switch>
</main>
<Footer />
</div>
</BrowserRouter>
);
}
}
export default MainPage;
//sideNavigation.js
import React from "react";
import logo from "../assets/logo_.svg";
import { MDBListGroup, MDBListGroupItem, MDBIcon } from "mdbreact";
import { NavLink } from "react-router-dom";
import "../styleFiles/sideBar.css";
const sideNavigation = () => {
return (
<div className="sidebar-fixed position-fixed">
<a href="#!" className="logo-wrapper waves-effect">
<img alt="MDB React Logo" className="img-fluid" src={logo} />
</a>
<MDBListGroup className="list-group-flush">
<NavLink exact={true} to="/" activeClassName="activeClass">
<MDBListGroupItem className="bgc">
<MDBIcon icon="chart-pie" className="mr-3" />
Testing Dashboard
</MDBListGroupItem>
</NavLink>
<NavLink to="/profile" activeClassName="activeClass">
<MDBListGroupItem className="bgc">
<MDBIcon icon="user" className="mr-3" />
Projects
</MDBListGroupItem>
</NavLink>
<NavLink to="/tables" activeClassName="activeClass">
<MDBListGroupItem className="bgc">
<MDBIcon icon="table" className="mr-3" />
Tables
</MDBListGroupItem>
</NavLink>
<NavLink to="/inventory" activeClassName="activeClass">
<MDBListGroupItem className="bgc">
<MDBIcon icon="fas fa-truck-loading" className="mr-3" />
Inventory
</MDBListGroupItem>
</NavLink>
<NavLink to="/404" activeClassName="activeClass" />
</MDBListGroup>
</div>
);
};
export default sideNavigation;
thanks in advance for your help guys...
When I made a small change to my code, I could eliminate that weird behavior.
the changes were only made to my App.js file.
please find the changes below
import React, { Component } from "react";
import { BrowserRouter, Route, Switch } from "react-router-dom";
import MainPage from "./Mainpage";
import LoginPage from "./components/pages/LoginPage";
import "./styleFiles/index.css";
class App extends Component {
render() {
return (
<div className="flexible-content ">
<Switch>
<Route path="/Main" exact component={MainPage} />
<Route path="/login" component={LoginPage} />
<Route component={MainPage} />
</Switch>
</div>
);
}
}
export default App;
I added a switch tag and a Generic Not Found route component to my app.js file.

ReactJS - I can't reach a component inside in other by route

I have an website with this structure:
<Header>
<MenuPrincipal>
<Home></Home>
</MenuPrincipal>
</Header>
When I click on a logo in the Header, it isn't working, nothing is happening and I would like to access the Home component The <Home> component is inside of <MenuPrincipal>. The <Header> component is the parent component, how you can see in the structure above. How can I solve it?
import React from 'react'
import { BrowserRouter as Router, Route, Link, } from 'react-router-dom'
import Home from './Home';
import {connect} from 'react-redux'
import {bindActionCreators} from 'redux'
import { showBGImage } from './actions';
const Header = (props) => {
return (
<div className="header">
<div>
<Router>
<div>
<h1>
<Link onClick={showBGImage} className="home-link" to='/'>Mauro Bitar<br />
<small className="home-link">arquiteto</small>
</Link>
</h1>
<Route exact path='/' render={() => <Home />} />
</div>
</Router>
</div>
</div>
)
}
export default Header
<BrowserRouter> it's more like a history browser, you need to use Switch instead.
import { BrowserRouter as Router, Route, Link, Switch} from 'react-router-dom'
const Header = (props) => {
return (
<div className="header">
<Router>
<Switch>
<div>
<h1>
<Link onClick={showBGImage} className="home-link" to='/'>Mauro Bitar<br />
<small className="home-link">arquiteto</small>
</Link>
</h1>
<Route exact path='/' render={() => <Home />} />
</div>
</Switch>
</Router>
</div>
)
}

Resources