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

262
Views
How to load active class for link in react

Created one simple tab for loading components, tabs are working fine but a "active" class loading for all Navlinks, on rendering. "active" class should load only for active tab. please help to fix this

import React, { useState } from 'react';
import { NavLink, Outlet } from "react-router-dom"; 
 
function SidenavSection(props) {
const [active, setActive] = useState("tab1")

    return (
        <div>          
            <div >
            <div sm="3" className='border sideNav'>
            <NavLink to="" className="btn btn-link"    onClick={() => setActive("tab1")}>Overview</NavLink> <br/>
            <NavLink to="" className="btn btn-link"  onClick={() => setActive("tab2")}>Align & Ratio</NavLink> <br/>
            <NavLink to="" className="btn btn-link"    onClick={() => setActive("tab3")}>Avatar</NavLink> <br/>       

                </div>
                <div sm="9" className='border'>                   
                    <div>
                    {active === "tab1" && <div>section component 1</div>}
                    {active === "tab2" && <div>section component 2</div>}
                    {active === "tab3" && <div>section component 3</div>}
               
            </div>
                </div>
            </div> 
        </div>
    );
}

export default SidenavSection;

screenshot output

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

You need to either use routing/conditional displaying using Javascript, here your trying to use both.

Without routing

import React, { useState } from 'react';
import { NavLink, Outlet } from "react-router-dom"; 
 
function SidenavSection(props) {
    const [active, setActive] = useState("tab1")

    const isActive = (key) => (active === key ? 'active' : '');

    return (
        <div>          
            <div >
            <div sm="3" className='border sideNav'>
            <a href="" className=`btn btn-link ${isActive('tab1')}`    onClick={() => setActive("tab1")}>Overview</a> <br/>
            <a href="" className=`btn btn-link ${isActive('tab2')}`  onClick={() => setActive("tab2")}>Align & Ratio</a> <br/>
            <a href="" className=`btn btn-link ${isActive('tab3')}`    onClick={() => setActive("tab3")}>Avatar</a> <br/>       

                </div>
                <div sm="9" className='border'>                   
                    <div>
                    {active === "tab1" && <div>section component 1</div>}
                    {active === "tab2" && <div>section component 2</div>}
                    {active === "tab3" && <div>section component 3</div>}
               
            </div>
                </div>
            </div> 
        </div>
    );
}

export default SidenavSection;
about 4 years ago · Juan Pablo Isaza Report

0

First of all, you don't need to use NavLink for this. You can achieve active class by adding a ternary operator for the classnames.

import React, { useState } from "react";
    import { NavLink } from "react-router-dom";
    export default function App() {
      const [active, setActive] = useState("tab1");
    
  return (
    <div>
      <div>
        <div sm="3" className="border sideNav">
          <p
            to=""
            className={`btn btn-link ${active === "tab1"? "active":""}`}
            onClick={() => setActive("tab1")}
          >
            Overview
          </p>{" "}
          <br />
          <p
            to=""
            className={`btn btn-link ${active === "tab2" && "active"}`}
            onClick={() => setActive("tab2")}
          >
            Align & Ratio
          </p>{" "}
          <br />
          <p
            to=""
            className={`btn btn-link ${active === "tab3" && "active"}`}
            onClick={() => setActive("tab3")}
          >
            Avatar
          </p>{" "}
          <br />
        </div>
        <div sm="9" className="border">
          <div>
            {active === "tab1" && <div>section component 1</div>}
            {active === "tab2" && <div>section component 2</div>}
            {active === "tab3" && <div>section component 3</div>}
          </div>
        </div>
      </div>
    </div>
  );
}

You should however try making an object or array including the links data, so you can easily loop through without much code, and setting active link also will be easier this way.

import React, { useState } from "react";
import { NavLink } from "react-router-dom";
export default function App() {
  const [active, setActive] = useState("0");
  const els = [
    {tab:1, name: "Overview",},
    {tab:2, name: "Avatar"},
    {tab:3, name: "Align & Ratio"},
  ]
  return (
    <div>
      <div>
        <div sm="3" className="border sideNav">
          {
            els.map((item, index)=>{
              return <p 
                onClick={()=>{setActive(index)}} 
                key={index}  
                className={index === active ? "active" : ""}
              >
                {item.name}
              </p>
            })
          }
          <br />
        </div>
        <div sm="9" className="border">
          <div>
            {active === "tab1" && <div>section component 1</div>}
            {active === "tab2" && <div>section component 2</div>}
            {active === "tab3" && <div>section component 3</div>}
          </div>
        </div>
      </div>
    </div>
  );
}
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!