React Router need help routing - reactjs

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>
);
}

Related

When a React button is clicked the url/route changes. But the content associated with the url/route does not render

I am new to react, and I have many videos, but I can not find my problem. I want to open the contact me section when I click the contact me button present on the main page. When I click on the contact me button, it changes the route in the URL but does the opens the page.
When I try to open the same page by clicking the link present in the navbar, it works.
I am cleaning up while posting the question to maintain readability.
This is my App.js
import React from 'react';
import Nav from './components/Nav';
// Import Pages
import AboutMe from './pages/AboutMe';
import MyWork from './pages/MyWork';
import ContactMe from './pages/ContactMe';
// Router
import {BrowserRouter, Route, Switch, useLocation } from "react-router-dom";
function App() {
const location = useLocation();
return (
<div className="App">
<GlobalStyle />
<Nav />
<BrowserRouter>
<Switch location={location} key={location.pathname}>
<Route path="/" exact>
<AboutMe />
</Route>
<Route path="/work" exact>
<MyWork />
</Route>
<Route path="/contact" exact>
<ContactMe />
</Route>
</Switch>
</BrowserRouter>
</div>
);
}
This is my AboutSection.
In my about section, when I click on my button, it enters /contact but does not open the page, but when I try to open the same route while clicking on the link from the navbar, it opens the link.
import React from "react";
import { Link } from "react-router-dom";
//Images
const AboutSection = () => {
return (
<About>
<Description>
{/*This is the button */}
<Link to="/contact">
<motion.button variants={fade}>
Contact Me
</motion.button>
</Link>
</Description>
<Image>
<motion.img variants={photoAnim} src={homeImg1} alt="camera guy" />
</Image>
<Wave />
</About>
);
};
const Hide = styled.div`
overflow: hidden;
`;
export default AboutSection;
Code of my Nav.js
import React from "react";
import { Link, useHistory } from "react-router-dom";
const Nav = () => {
return (
<StyledNav>
<h1>
<Link id="logo" to="/">
Nitish Poonia
</Link>
</h1>
<ul>
<li>
<Link to="/contact">Contact Me</Link>
</li>
</ul>
</StyledNav>
);
};
export default Nav;
My Contact me
import React from "react";
import styled from "styled-components";
//Animation
import { motion } from "framer-motion";
import { pageAnimation, titleAnim } from "../animation";
const ContactUs = () => {
return (
<ContactStyle>
<Title>
<Hide>
<motion.h2 variants={titleAnim}>Get in touch.</motion.h2>
</Hide>
<div className="line2"></div>
</Title>
<div>
<Hide>
<Social variants={titleAnim}>
<Circle />
<h2>Socials</h2>
</Social>
</Hide>
<Hide>
<Social variants={titleAnim}>
<Circle />
<h2>Send me a message</h2>
</Social>
</Hide>
<Hide>
<Social variants={titleAnim}>
<Circle />
<h2>Drop an email.</h2>
</Social>
</Hide>
</div>
</ContactStyle>
);
};
export default ContactUs;
I think there can be a few changes you can do towards fixing the issues. Noting them down below.
Switch component should not be passed any props, you don't need to get location here using useLocation and pass it there and same for key as well. Refer React Router Switch Link
Can you change the order of your routes to look like something below just to be safe. We are putting the / i.e. the base route at the end, so that it acts as a fallback too.
<BrowserRouter>
<Switch>
<Route path="/work" exact>
<MyWork />
</Route>
<Route path="/contact" exact>
<ContactMe />
</Route>
<Route path="/">
<AboutMe />
</Route>
</Switch>
</BrowserRouter>

NavLink does not add activeClassName (react-router-dom v5.2.0)

I have a small problem with Nav Link.
It doesn't apply active Class Name, although it otherwise works (goes to the desired path, renders the required component)
react-router-dom version 5.2.0
my index.js
import React from 'react';
import ReactDOM from 'react-dom';
import { BrowserRouter } from 'react-router-dom';
import './index.css';
import App from './components/App';
ReactDOM.render(
<React.StrictMode>
<BrowserRouter>
<App />
</BrowserRouter>
</React.StrictMode>,
document.getElementById('root')
);
and my Navigation.js:
import { NavLink } from 'react-router-dom';
function Navigation() {
return (
<nav className="navigation">
<ul className="navigation__list">
<li>
<NavLink to="/" activeClassName="navigation__item_active" className="navigation__item">
Home
</NavLink>
</li>
<li>
<NavLink to="/frontend" className="navigation__item" activeClassName="navigation__item_active">
frontend
</NavLink>
</li>
<li>
<NavLink to="/about" className="navigation__item" activeClassName="navigation__item_active">
about
</NavLink>
</li>
</ul>
</nav>
);
}
export default Navigation;
UPD:
Router works, trouble in NaVLink.
I need to add activeClassName to nav item, but it doesnt.
my App.js:
import React from 'react';
import Header from './Header';
import Footer from './Footer';
import Main from './Main';
function App() {
return (
<div className="App">
<Header/>
<Main/>
<Footer/>
</div>
);
}
export default App;
and Main.js:
import React from 'react';
import { Route } from 'react-router-dom';
import HelloPage from './Hello-page';
import About from './About';
import Frontend from './Frontend';
import Navigation from './Navigation';
function Main() {
return (
<main className="main">
<Route exact path="/">
<HelloPage/>
</Route>
<Route path="/about">
<About/>
</Route>
<Route path="/frontend">
<Frontend/>
</Route>
<Navigation/>
</main>
);
}
export default Main;
it fixed by 1 command... (10+ hours of my live)
npm i path-to-regexp#1.7.0 -S
It should already work tho~
Btw, even if you applied those activeClassName. You still need to tweak your code in App.js to something like this:-
need to add Redirect only for the home or / route. If not, the activeClassName will be applied always for home or / route
App.js:-
export default function App() {
return (
<Router>
<Nav />
<Switch>
{/* Add this Redirect */}
<Redirect exact from="/" to="/home" />
<Route path="/home" component={() => "Home page"} />
<Route path="/demo" component={() => "Demo page"} />
<Route path="/demo2" component={() => "Demo2 page"} />
</Switch>
</Router>
);
}
You can see this working sandbox
Try to delete the Redirect and see how it changes
As mentioned here in my case solution was replace in webpack
resolve: {
modules: [
path.resolve('./src'),
- path.resolve('./node_modules'),
+ 'node_modules',
],
},

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>
);
}

Unable to see the text returned from home and tutorials page in react-hooks site

Unable to see the header text returned in my Home and Tutorials page in my react hooks site. Below is my App.js, Navigation.js and Home.js. Could someone please advise about the problem here.
App.js
import React from 'react';
import { BrowserRouter, Route, Switch } from "react-router-dom";
import './App.css';
import Home from "./components/Home";
import Tutorials from "./components/Tutorials";
import Navigation from './components/Navigation';
function App() {
return (
<BrowserRouter>
<Navigation>
<Switch>
<Route exact path="/" component={Home} />
<Route path="/tutorials" component={Tutorials} />
</Switch>
</Navigation>
</BrowserRouter>
);
};
export default App;
Navigation.js
import React from 'react';
import { NavLink} from 'react-router-dom';
const Navigation = () => {
return (
<div className="App">
<div className="wrapper">
<div id="wrap">
<nav className="siteNavigation_nav_links">
<div className="row" ></div>
<div className="main_links_nav">
<NavLink className="mob_link" to="/">Home</NavLink>
<NavLink className="mob_link" to="/tutorials">Tutorials</NavLink>
</div>
</nav>
</div>
</div>
</div>
)
}
export default Navigation;
Home.js
import React from 'react';
const Home = () =>{
return (
<div className="App">
<h1>This is Home Page !</h1>
</div>
)
}
export default Home;
Tutorials.js
import React from 'react';
const Tutorials = () =>{
return (
<div><h1>This is Tutorials Page !</h1></div>
)
};
export default Tutorials;
Navigation is a Navbar component. And you want it everywhere on the site.
So, you can easily do it by using it outside the scope of the Switch component.
In doing so, it will render all over your screens.
<BrowserRouter>
<Navigation />
<Switch>
<Route exact path="/" component={Home} />
<Route path="/tutorials" component={Tutorials} />
</Switch>
</BrowserRouter>

React-Router DOM not rendering the content from other pages

// 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;

Resources