Problem: isActive
(add active class to link) is not working.
My NavLink component is not working. I posted this problem several times in stack overflow no one solved my problem instead of solving they are tagging another question which doesn't solve my problem.
This is the home component:
import React from 'react';
// import Nav from './navbar';
import { NavLink } from "react-router-dom";
import "../App.css";
export default function home() {
return (
<div>
<NavLink
to="/"
style={isActive => ({
color: isActive ? "green" : "blue"
})}
>
home
</NavLink>
<NavLink
to="/about"
style={isActive => ({
color: isActive ? "green" : "blue"
})}
>
about
</NavLink>
<h1>Home page Here</h1>
</div>
)
}
This is the "About" component:
import React from 'react';
// import Nav from './navbar';
import { NavLink } from 'react-router-dom';
import "../App.css";
export default function about() {
return (
<>
<NavLink
to="/"
style={isActive => ({
color: isActive ? "green" : "blue",
backgroundColor: isActive ? "red" : "white"
})}
>
home
</NavLink>
<NavLink
to="/about"
style={isActive => ({
color: isActive ? "green" : "blue",
backgroundColor: isActive ? "red" : "white"
})}
>
about
</NavLink>
<h1>About page here</h1>
</>
)
}
I tried so many times and so many ways but the result is always same shown in above picture. I want isActive
feature to work fine and should add active class when I click on about or anything else in navbar