Problem with React Router NavLink and activeClassName - css

My First problem is that, I have three links in menu. Home, About and Add Food. NavLink activeClassName works as expected , except for this : Home always stays active, even when i move to other links.
In the screenshot, i've moved to /about page, but Home *li stays active.
Secondly, for some reason, border radius doesn't work on these items. Is it because they have substantial padding?
my code :
import React from "react";
import { NavLink } from "react-router-dom";
const Navbar = (props) => {
return (
<nav>
<ul>
<NavLink to="/" activeClassName="active-navlink">
<li>Home</li>
</NavLink>
<NavLink to="/about" activeClassName="active-navlink">
<li>About</li>
</NavLink>
<NavLink to="/add-food" activeClassName="active-navlink">
<li>Add Food</li>
</NavLink>{" "}
</ul>
</nav>
);
};
export default Navbar;
and my Router, where all the links are handled :
<Router>
{" "}
<div className="App">
<Navbar></Navbar>
<Switch>
<Route path="/about" component={About}></Route>
<Route
path="/products/:food"
render={(props) => (
<IndividualProduct {...props}></IndividualProduct>
)}
></Route>
<Route
path="/"
exact
render={(props) => (
<Home
{...props}
groceryList={groceryList}
orderTotal={orderTotal}
setOrderTotal={setOrderTotal}
productsInCart={productsInCart}
updateProductsInCart={updateProductsInCart}
></Home>
)}
></Route>
<Route component={NotFound}></Route>
</Switch>
</div>
</Router>

1.
Add exact to Navlink of '/':
<NavLink to="/" activeClassName="active-navlink" exact={true} >
<li>Home</li>
</NavLink>
Styling sample:
https://codepen.io/k3no/pen/OXgXOb

Related

Home loads under navabr

import Home from './components/Home/Home.jsx'
import Navbar from './components/Navbar/Navbar'
import {BrowserRouter, Routes, Route} from "react-router-dom"
export default function App() {
return (
<BrowserRouter>
<Navbar />
<div className="container">
<Routes>
<Route path="/" element={<Home />} />
</Routes>
</div>
</BrowserRouter>
);
}
App.css:
.container {
flex: 1;
display: flex;
flex-direction: column;
}
Navbar CSS
import React from "react";
import cart from "../assets/images/cart.png"
import { TfiMenu } from "react-icons/tfi";
import { AiOutlineCloseCircle} from "react-icons/ai";
export default function Navbar() {
const [open, setopen] = React.useState(false);
let Links = [
{name:"Home" ,link:"/"},
{name:"Headphone" ,link:"/"},
{name:"Speaker" ,link:"earphones"},
{name:"Earphone" ,link:"/"},
];
const change= ()=> {
setopen(!open);
}
return (
<>
<div className=" fixed shadow-md w-full top-0 left-0 ">
<div className=" lg:flex items-center bg-black py-4 md:justify-around">
<div className=" flex justify-between px-3 pt-2 text-white cursor-pointer font-[Poppins]">
audiophile
<div onClick={change} className=" lg:hidden w-9 inline-block">
{
open
? (<span className=""><AiOutlineCloseCircle/></span>)
: (<span className=""><TfiMenu/></span>)
}
</div>
</div>
<ul className={`lg:flex md:items-center ${open ?"" :"hidden"} `}>
{ Links.map((link) => (
<li className=' md:ml-8 text-xl md:my-0 my-7'>
<a href={link.link} className='text-white hover:text-yellow-500 duration-500'>{link.name}</a>
</li>
))}
</ul>
<div className>
<img className="absolute right-20 md:inline-block w-7 cursor- top-5 " src={cart} alt="cart" />
</div>
</div>
</div>
</>
);
}
this is my code and whenever I try to load the home top portion is always under the navbar. .my navbar is positioned sticky. also can't make my home width to 100. maybe because it is under another component. to render it properly i am using margin-top (mt-44) or else its under navabr Any help will be really appreciated.
you should move your container one level up and move your navbar out of it like this:
import Home from './components/Home/Home.jsx'
import Earphones from './components/Earphones/Earphones'
import Navbar from './components/Navbar/Navbar'
import {BrowserRouter,Routes,Route} from "react-router-dom"
export default function App()
{
return(
<div className="container">
<Navbar/>
<BrowserRouter>
<div className="anothercontainer">
<Routes>
<Route path="/" element={<Home />} />
</Routes>
</BrowserRouter>
</div>
</div>
)
}

How to make links in <NavBar> float right using react-boostrap

I would like all the elements nested inside of the first Nav tag to be on the left side of the NavBar, and all in the second Nav tag to float right. How can I achieve this using react Bootstrap?
I tried doing Nav className="float-right" on the second Nav tag but it didn't work. I also tried className="ml-auto".
<Nav>
<Nav.Link>
{" "}
<Link
to="/booklets"
className="nav-link"
onClick={() => setExpanded(false)}
>
Booklets
</Link>
</Nav.Link>
<Nav.Link>
{" "}
<Link
to="/form"
className="nav-link"
onClick={() => setExpanded(false)}
>
Upload New Booklet
</Link>
</Nav.Link>
<Nav.Link>
{" "}
<Link
to="/about"
className="nav-link"
onClick={() => setExpanded(false)}
>
About
</Link>
</Nav.Link>
</Nav>
<Nav>
<Nav.Link>
{" "}
<Link
to="/login"
className="nav-link"
onClick={() => {
setExpanded(false);
currentUser && logout();
}}
>
{currentUser ? "Logout" : "Login"}
</Link>
</Nav.Link>
<Nav.Link>
{" "}
<Link
to="/register"
className="nav-link"
onClick={() => setExpanded(false)}
>
{!currentUser ? "Register" : ""}
</Link>
</Nav.Link>
</Nav>
</Navbar.Collapse>
</Navbar>
Assuming that the direct parent of the two <Nav> is <Navbar.Collapse>, perhaps try style the parent to have it place the items each on one side, with flex display.
Example with Bootstrap classes:
<Navbar.Collapse className="d-flex justify-content-between">
Example with inline styles:
<Navbar.Collapse style={{ display: 'flex', justifyContent: 'space-between' }}>

React Location Router doesn't work after changing state in Redux

I'm using #tanstack/react-location in React Typescript project + 'react-redux'.
When I change redux auth state (login, logout) I also make my MainNavigation changing. Some Links are added or removed. And when it happens the router stops working.
Updated Links are working, url is changing, but Router isn't responding.
After refresh Router works again.
App.tsx
...
<Router
location={reactLocation}
routes={appRoutes}
defaultPendingElement={<Spinner />}
defaultPendingMs={10}
>
<MainNavigation />
<Outlet />
</Router>
MainNavigation.tsx
<header className="header">
<Link to="/" getActiveProps={getActiveProps}>
<div className="logo">RS Lang</div>
</Link>
<nav>
<ul>
<li>
<Link to="/" getActiveProps={getActiveProps}>
Home
</Link>
</li>
<li>
<Link to="/team" getActiveProps={getActiveProps}>
Team{' '}
</Link>
</li>
<li>
<Link to="/textbook" getActiveProps={getActiveProps}>
Textbook
</Link>
</li>
{isLoggedIn && (
<li>
<Link to="/dictionary" getActiveProps={getActiveProps}>
Dictionary
</Link>
</li>
)}
<li>
<Link to="/games" getActiveProps={getActiveProps}>
Games
</Link>
</li>
{!isLoggedIn && (
<li>
<Link to="/auth" getActiveProps={getActiveProps}>
Login|SignUp
</Link>
</li>
)}
{isLoggedIn && (
<li>
<Link to="/profile" getActiveProps={getActiveProps}>
Profile {authState.user.name}
</Link>
</li>
)}
{isLoggedIn && (
<li>
<button type="button" onClick={logoutHandler}>
Logout
</button>
</li>
)}
</ul>
</nav>
</header>
for me the issue was that the definition of the ReactLocation was not in the main.tsx. So I fixed it like so:
main.tsx
export const reactLocation = new ReactLocation();
ReactDOM.createRoot(document.getElementById('root')!).render(<App />);
App.tsx
import { reactLocation} from './main.tsx';
export const App = () => {
return <Router
location={reactLocation}
routes={appRoutes}
defaultPendingElement={<Spinner />}
defaultPendingMs={10}
>
<MainNavigation />
<Outlet />
</Router>
}
Hope that helps :)
(I was using vite as build tool)

How do I change the whole page background color with each route using React?

Im building a react app and I want every route to have a different background color.
But the only way to change the background color of the whole page is by using the * {} tag. Is there some kind of theme library or anything I can do to pass a variable from my routes to the * {} tag to change its background color?
Other solutions would include putting a min-height on my root component but that just feels wrong and I will not do it.
I think you can use this as well
function App() {
const [color, changeColor] = useState("#282c34");
document.body.style.backgroundColor = color;
return (
<div>
<Router>
<div>
<nav>
<ul>
<li>
<Link to="/" onClick={() => changeColor("#282c34")}>
Home
</Link>
</li>
<li>
<Link to="/about/" onClick={() => changeColor("#9c27b0")}>
About
</Link>
</li>
<li>
<Link to="/users/" onClick={() => changeColor("#007bff")}>
Users
</Link>
</li>
</ul>
</nav>
<Route path="/" exact component={Index} />
<Route path="/about/" component={About} />
<Route path="/users/" component={Users} />
</div>
</Router>
</div>
);
}
You can use a state color and pass it to each Route.
This is an exemple to illustrate what i ve told you :
function App() {
const [color, changeColor] = useState("#282c34");
return (
<div style={{ background: color }} id="main">
<Router>
<div>
<nav>
<ul>
<li>
<Link to="/" onClick={() => changeColor("#282c34")}>
Home
</Link>
</li>
<li>
<Link to="/about/" onClick={() => changeColor("#9c27b0")}>
About
</Link>
</li>
<li>
<Link to="/users/" onClick={() => changeColor("#007bff")}>
Users
</Link>
</li>
</ul>
</nav>
<Route path="/" exact component={Index} />
<Route path="/about/" component={About} />
<Route path="/users/" component={Users} />
</div>
</Router>
</div>
);
}

React-Bootstrap CSS issue

When I import Bootstrap CSS it adds extra CSS to the navbar. As it is shown in the image below. I am using bootstrap cards in this code but not for Navbar. When I remove the Bootstrap CSS It works fine.
You can check the code here
Please help me resolve this.
if your problem is the under line you can omit that using
style={{ textDecoration: 'none' }}
here is your modified navbar.jsx file
// eslint-disable-next-line
import React from 'react';
import { NavLink } from 'react-router-dom';
function Navbar(){
return(
<>
<header className="l-header">
<div className="navi bd-grid">
<div>
<NavLink to="#" className="navi__logo">Portfolio</NavLink>
</div>
<div className="navi__menu" id="navi-menu">
<ul className="navi__list">
<li className="navi__item">
<NavLink to="#home" className="navi__link menu" style={{ textDecoration: 'none' }}>Home</NavLink>
</li>
<li className="navi__item">
<NavLink to="#about" className="navi__link" style={{ textDecoration: 'none' }}>About</NavLink>
</li>
<li className="navi__item">
<NavLink to="#skills" className="navi__link" style={{ textDecoration: 'none' }}>Skills</NavLink>
</li>
<li className="navi__item">
<NavLink to="#work" className="navi__link" style={{ textDecoration: 'none' }}>Work</NavLink>
</li>
<li className="navi__item">
<NavLink to="#contact" className="navi__link" style={{ textDecoration: 'none' }}>Contact</NavLink>
</li>
</ul>
</div>
<div className="navi__toggle" id="navi-toggle">
<i className='bx bx-menu'></i>
</div>
</div>
</header>
</>
);
};
export default Navbar;
output will be something like this
Try to import the bootstrap on your index.js file and it should solve the problem.

Resources