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

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',
],
},

Related

react router not working and no erros in console or compilation error is shown [ react router dom v5]

Until now the router was working perfectly fine but suddenly it isn't working. I'm totally confused and I haven't done any kind of upgrades.
The router doesn't render the components itself. I wonder if even the correct route gets rendered based on path.
Only the first route is working fine, the other two don't seem to work.
AssetPage.js
import React from "react";
import { Route } from "react-router-dom";
import AssetNavBar from "./AssetNavBar";
import "./assets.css";
function AssetPage() {
return (
<div className="top-div">
<header>
<AssetNavBar />
</header>
<main className="main-flex">
<Route path="/assets/add" exact><h1>in main</h1></Route>
<Route path="/assets/storage">
<h1>in storage</h1>
</Route>
<Route path="/assets/view">
<h1>in view</h1>
</Route>
</main>
</div>
);
}
export default AssetPage;
AssetNavBar.js
import React from "react";
import {Link} from 'react-router-dom';
function AssetNavBar() {
return (
<div>
<nav>
<ul className="ul-side-by-side">
<li>
<Link to="/assets/add">add assets</Link>
</li>
<li>
<Link to="/assets/view">view assets</Link>
</li>
<li>
<Link to="/assets/storage">storage</Link>
</li>
</ul>
</nav>
</div>
);
}
export default AssetNavBar;
index.js - [starting point]
import React from 'react';
import ReactDOM from 'react-dom';
import './index.css';
import App from './App';
import {BrowserRouter} from "react-router-dom";
ReactDOM.render(
<BrowserRouter>
<App />
</BrowserRouter>,
document.getElementById('root')
);
App.js
import "./App.css";
import { Route } from "react-router-dom";
import SingleItem from "./components/inventory/SingleItem"
import HomeNavBar from "./components/HomeNavBar";
import InventoryPage from "./components/inventory/InventoryPage";
import ProductPage from "./components/products/ProductPage";
import PurchasePage from "./components/purchase/PurchasePage";
import SupplierPage from "./components/supplier/SupplierPage";
import AssetPage from "./components/assetFolder/AssetPage";
function App() {
return (
<div className='horizontal-div-boxes'>
<header>
<HomeNavBar/>
</header>
<main className="main-flex">
<Route path="/dashboard">
<SingleItem />
</Route>
<Route path='/inventory'>
<InventoryPage/>
</Route>
<Route path='/purchase'>
<PurchasePage/>
</Route>
<Route path='/products/add'>
<ProductPage/>
</Route>
<Route path='/suppliers/add'>
<SupplierPage/>
</Route>
<Route path='/assets/add'>
<AssetPage/>
</Route>
</main>
</div>
);
}
export default App;
version
└─┬ react-router-dom#5.3.0
└── react-router#5.2.1
Issue
The AssetPage component is rendered on a "/assets/add" path and there are no other "/assets*" paths, so the other two links don't link to any routes that render content.
<Route path='/assets/add'>
<AssetPage />
</Route>
In other words, when you link to "/assets/view" or "/assets/storage" the path no longer matches "/assets/add" and the AssetPage component is unmounted.
Solution
I am sure that you want this root route to be just "/assets" to allow further matching any "/assets*" paths.
<Route path='/assets'>
<AssetPage />
</Route>

basic components not rendering

I'm just starting my first react web app but my components are not rendering. even a simple h1 element directly in the app.js file is not rendering. it did, however, after deleting the Routes, so my best guess is that the problem lies somewhere there.
the code:
app.js
import React, { lazy, Suspense } from 'react'
import './App.css';
import { Routes, Route } from 'react-router-dom'
import { ToastContainer } from 'react-toastify'
import 'react-toastify/dist/ReactToastify.css'
const Home = React.lazy(() => import('./pages/Home'))
const App = () => {
return (
<Suspense fallback={<div>Loading...</div>}>
<h1>App</h1>
<ToastContainer />
<Routes>
<Route exact path="/" element={Home} />
</Routes>
</Suspense>
)
}
export default App;
Home.js
import React from 'react'
const Home = () => {
return (
<h1>Home Page</h1>
)
}
export default Home
sorry for this very beginner question. please keep in mind that this is my first react web app.
React route accepts ReactElement not ReactNode, no need of exact also
<Routes>
<Route path="/" element={<Home />} />
</Routes>
I think that the tutorial you are using uses the previous react-router API. Please, try this:
import React from "react";
import {
BrowserRouter as Router,
Switch,
Route,
Link,
useRouteMatch,
useParams
} from "react-router-dom";
export default function App() {
return (
<Router>
<div>
<ul>
<li>
<Link to="/">Home</Link>
</li>
<li>
<Link to="/about">About</Link>
</li>
<li>
<Link to="/topics">Topics</Link>
</li>
</ul>
<Switch>
<Route path="/about">
<About />
</Route>
<Route path="/">
<Home />
</Route>
</Switch>
</div>
</Router>
);
}
function Home() {
return <h2>Home</h2>;
}
function About() {
return <h2>About</h2>;
}

React: Browser donot load the page by self until i refresh the page

Actually, the problem is when I click on the link the browser don't get the page automatically I always need to refresh then it loads that page, I am new in react so is anyone know how I can rid of this. Thanks in advance
here is my navigation bar code
function MainNavigation() {
return (
<BrowserRouter>
<header>
<div>React Meets Up</div>
<nav>
<ul>
<li>
<Link to="/">AllMeetUp</Link>
</li>
<li>
<Link to="/newmeetup">New MeetUp</Link>
</li>
<li>
<Link to="/favorites">Favourite Page</Link>
</li>
</ul>
</nav>
</header>
</BrowserRouter>
);
}
export default MainNavigation;
here is my app.js code
import { BrowserRouter, Route } from "react-router-dom";
import { Routes } from "react-router-dom";
import MainNavigation from "./components/Layout/MainNavigation";
import AllMeetsUpPage from "./Pages/AllMeetUp";
import FavouritesPage from "./Pages/Favourite";
import NewMeetUpPage from "./Pages/NewMeetUp";
function App() {
return (
<div>
<MainNavigation />
<Routes>
<Route path="/" element={<AllMeetsUpPage />} />
<Route path="/newmeetup" element={<NewMeetUpPage />} />
<Route path="/favorites" element={<FavouritesPage />} />
</Routes>
</div>
);
}
export default App;
and finally here is my index.js code
import ReactDOM from "react-dom";
import {BrowserRouter} from 'react-router-dom';
import "./index.css";
import App from "./App.js";
ReactDOM.render(
<BrowserRouter>
<App />
</BrowserRouter>,
document.getElementById("root")
);
I solve this problem by uninstalling the react-router-dom first and then install v5.2.0
I also remove the browser routing tag at the main navigation

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

How can I redirect users to different pages with Link

I have the code like bellow. I want when I click in the link it should direct me to the other page.
But It doesn't do anything. Could someone please help with this?
<Link className="portfolio-link" data-toggle="modal" to={`/blog/${this.props.post.slug}`}>
<div className="portfolio-hover">
<div className="portfolio-hover-content">
<i className="fas fa-plus fa-3x"></i>
</div>
</div>
{this.props.post.PostImage ? this.props.post.PostImage.length > 0 ? <img style={{width: '100%'}} className="img-fluid" src={API.makeFileURL(this.props.post.PostImage[0].thumbnail, null)} alt="/"/>
: null: null}
</Link>
You may create separate component for navigation purpose where you may put all of your url links
NavBar component
import React from "react";
import { Link } from "react-router-dom";
const NavBar = ({ menuData }) => {
return (
<nav>
<ul>
{menuData.map(menu => {
return (
<li>
<Link to={menu.path}>
{menu.name}
</Link>
</li>
);
})}
</ul>
</nav>
);
};
export default NavBar;
You have to ensure the route section, this section specially manage your route and it will redirect you to your desired page/component based on url change
<Switch>
<Route exact path="/login" component={Login} />
<Route exact path="/" component={Home} />
</Switch>
So I keep the NavBar and the route closely in App.js
App.js
import React, { Component } from "react";
import NavBar from "./navbar";
import { Route, Redirect, Switch } from "react-router-dom";
import "./App.css";
class App extends Component {
state = {
navBars: [
{ name: "Home", path: "/" },
{ name: "Login", path: "/login" }
]
};
render() {
return (
<React.Fragment>
<main>
<NavBar
menuData={this.state.navBars}
></NavBar>
<div className="content">
<Switch>
<Route exact path="/login" component={Login} />
<Route exact path="/" component={Home} />
</Switch>
</div>
</main>
</React.Fragment>
);
}
}
export default App;
And finally you have to make sure your navbar/Link and Routes should be under Router, So BrowserRouter confirm this thing
<BrowserRouter>
<App />
</BrowserRouter>
You may have a look in my index.js
index.js
import React from "react";
import ReactDOM from "react-dom";
import App from "./App";
import { BrowserRouter} from "react-router-dom";
import "./index.css";
ReactDOM.render(
<BrowserRouter>
<App />
</BrowserRouter>,
document.getElementById("root")
);
Hope it will help you

Resources