Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

117
Views
I Want To Add Specific Styles To An Navbar Element When Clicking On It

I Tried The UseState(false) But It Added The Styles To All The Other Navbar Elements

import React, {useState } from 'react';
import { AiOutlineMenu } from 'react-icons/ai';
import { Navbar, NavContainer, NavLogo, NavLogoLink, NavMobileIcon, NavLists, NavList, NavLink, NavBtn, NavBtnLink } from './NavStyle';

export default function Nav({openHandler}) {

    const [isActive, setIsActive] = useState(false);

    const clickHandler = () => {
        setIsActive(!isActive);
    }

    return (
        <Navbar>
            <NavContainer>
                <NavLogo><NavLogoLink to="/about">Jonathan</NavLogoLink></NavLogo>
                <NavMobileIcon onClick={openHandler}>
                    <AiOutlineMenu />
                </NavMobileIcon>
                <NavLists>
                    <NavList onClick={clickHandler} className={isActive ? 'active' : ''}>
                        <NavLink to="#about">About</NavLink>
                    </NavList>
                    <NavList onCLick={clickHandler} className={isActive && 'active'}>
                        <NavLink to="#discover">Discover</NavLink>
                    </NavList>
                    <NavList onClick={clickHandler} className={isActive && 'active'}>
                        <NavLink to="#services">Services</NavLink>
                    </NavList>
                </NavLists>
                <NavBtn><NavBtnLink to="signup">Sign Up</NavBtnLink></NavBtn>
            </NavContainer>
        </Navbar>
    )
}
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

Instead of a boolean in useState you should store an identifier unique to the NavLink that is activated.

import React, {useState } from 'react';
import { AiOutlineMenu } from 'react-icons/ai';
import { Navbar, NavContainer, NavLogo, NavLogoLink, NavMobileIcon, NavLists, NavList, NavLink, NavBtn, NavBtnLink } from './NavStyle';

export default function Nav({openHandler}) {

    const [isActive, setIsActive] = useState('');

    const clickHandler = (e) => {
        setIsActive((isActive) => isActive = e.currentTarget.id);
    }

    return (
        <Navbar>
            <NavContainer>
                <NavLogo><NavLogoLink to="/about">Jonathan</NavLogoLink></NavLogo>
                <NavMobileIcon onClick={openHandler}>
                    <AiOutlineMenu />
                </NavMobileIcon>
                <NavLists>
                    <NavList onClick={clickHandler} id="about" className={isActive === 'about' && 'active'}>
                        <NavLink to="#about">About</NavLink>
                    </NavList>
                    <NavList onCLick={clickHandler} id="discover" className={isActive === 'discover' && 'active'}>
                        <NavLink to="#discover">Discover</NavLink>
                    </NavList>
                    <NavList onClick={clickHandler} id="services" className={isActive === 'services' && 'active'}>
                        <NavLink to="#services">Services</NavLink>
                    </NavList>
                </NavLists>
                <NavBtn><NavBtnLink to="signup">Sign Up</NavBtnLink></NavBtn>
            </NavContainer>
        </Navbar>
    )
}

Disclaimer: This code might not run successfully, because I didn't run it in the first place. It is merely a hint to guide you in the right direction.

Another approach would be a useRef hoof in order to store an identifier of the NavList that has been clicked and access that identifier inside clickHandler

about 4 years ago · Juan Pablo Isaza Report

0

You have same click handler for all the NavList components. So when you click any of them the handler will toggle isActive prop. As all the className conditions uses the same isActive prop it will change class of all them. You have to figure out which one was clicked and is active.

import React, {useState } from 'react';
import { AiOutlineMenu } from 'react-icons/ai';
import { Navbar, NavContainer, NavLogo, NavLogoLink, NavMobileIcon, NavLists, NavList, NavLink, NavBtn, NavBtnLink } from './NavStyle';

export default function Nav({openHandler}) {

    const [active, setActive] = useState('about'); // about is default value

    const clickHandler = (name) => () => {
        setActive(name);
    }

    return (
        <Navbar>
            <NavContainer>
                <NavLogo><NavLogoLink to="/about">Jonathan</NavLogoLink></NavLogo>
                <NavMobileIcon onClick={openHandler}>
                    <AiOutlineMenu />
                </NavMobileIcon>
                <NavLists>
                    <NavList onClick={clickHandler('about')} className={active === 'about' ? 'active' : ''}>
                        <NavLink to="#about">About</NavLink>
                    </NavList>
                    <NavList onCLick={clickHandler('discover')} className={active === 'discover' && 'active'}>
                        <NavLink to="#discover">Discover</NavLink>
                    </NavList>
                    <NavList onClick={clickHandler('services')} className={active === 'services' && 'active'}>
                        <NavLink to="#services">Services</NavLink>
                    </NavList>
                </NavLists>
                <NavBtn><NavBtnLink to="signup">Sign Up</NavBtnLink></NavBtn>
            </NavContainer>
        </Navbar>
    )
}
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!