Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

activeClassName react router

// Before React Router v6, to set class for active element we used:
 <NavLink to="/start" activeClassName="HERE NAME CLASS WHEN COMPONENT IS ACTIVE">start</NavLink>

// Now, instead of activeClassName="HERE..." use:
className={(navLinkObj) => "start-selected_" + navLinkObj.isActive}

// And then in css use:
.start-selected_true {
  background-color: red;
  border-bottom: 5px solid yellow;
}

// Also, you can do the same for inactive link. In css use:
.start-selected_false {
  background-color: blue;
  border-bottom: 5px dashed green;
}

// PS: (navLinkObj.isActive) returns true if element is active and false if element is not
// PS: I really advice you to check out documentation of React Router. It helped me :)
// React Router Documentation: https://v5.reactrouter.com/web/api
Comment

activeclassname in react router v6

export function Header() {
  return (
    <header>
      <ul>
        <li>
          <NavLink className={(navData) => navData.isActive ? "active" : "" } to="/about" />
        </li>
        <li>
          <NavLink className={(navData) => navData.isActive ? "active" : "" } to="/profile" />
        </li>
      </ul>
    </header>
  )
}
Comment

PREVIOUS NEXT
Code Example
Javascript :: js add style to each class 
Javascript :: string to title case javascript 
Javascript :: get caret position javascript 
Javascript :: bootstrap multiselect change value 
Javascript :: how to check what browser you are using javascript 
Javascript :: eslint ignore 
Javascript :: padend method javascript 
Javascript :: eof while parsing 
Javascript :: scroll into view 
Javascript :: what can i delete from create-react-app 
Javascript :: deep copy object/array 
Javascript :: npm install run audit fix 
Javascript :: get pods running on a node 
Javascript :: convert a string to an integer in javascript 
Javascript :: loop array and check if value matches in js 
Javascript :: isempty is not defined 
Javascript :: form data append jquery 
Javascript :: prime or not in javascript 
Javascript :: How to insert divider in react native 
Javascript :: loopback model count 
Javascript :: padstart and padend javascript 
Javascript :: javascript get random line from text file 
Javascript :: read csv file in javascript 
Javascript :: integers to space separated string in javascript 
Javascript :: ajax get form data 
Javascript :: cheerio load from file 
Javascript :: javascript key pressed enter 
Javascript :: count div class in div jquery 
Javascript :: How to install react native hooks with npm 
Javascript :: nested shorthand if javascript 
ADD CONTENT
Topic
Content
Source link
Name
9+2 =