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

218
Views
Style div in React's jsx as per the id/ class name, which will check condition

So, here is what exactly I am trying to do...
I am sending the class name and id using props. and by checking the condition the particular style will be implemented to it.
(this code is used to implement Navigation bar)
so that my active class (Menu Tab) will be highlighted only.

but, when I used this it will highlight all navigation tabs at the same time.

export default function NavElements(props) {

    const history = useLocation();
    // console.log(history);

    const [isActive, setIsActive] = useState(props.isActive);

    useEffect(() => {
        setIsActive(props.isActive);
    },[props.isActive]);

    function handleClick() {
        props.onClick(props.index);
    }
    return (
        <Link to={`${props.to}`}>
            <div id={`${props.rectangle}`} onClick={handleClick} className={ isActive ? 'backgroundSelected' : ''}>
                <div className="buttons-style" id={`${props.myClassName}`}>
                    <div className="text-align">
                        <div className="text-link">{props.Title}</div>
                    </div>
                </div>
            </div>
        </Link>
    )
}
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

You can also use useLocation as you defined also

export default function NavElements(props) {

    let location = useLocation();
    // console.log(history);


    return (
        <Link to={`${props.to}` className={`${location.pathname === props.to }? "active":""`}}>
            <div id={`${props.rectangle}`}>
                <div className="buttons-style" id={`${props.myClassName}`}>
                    <div className="text-align">
                        <div className="text-link">{props.Title}</div>
                    </div>
                </div>
            </div>
        </Link>
    )
}

The Logic is whenver the pathname equals to the endpoint directed by any <Link> , it gets active.

You can define your own class inplace of active class of if you want

about 4 years ago · Juan Pablo Isaza Report

0

I'm a little bit confused about what you are trying to do. But if you want highlight the active menu, why can't you use NavLink instead of Link?. The NavLink has a property isActive which return true if the menu is active.

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!