Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to manage active state in bootstrap navbar in react? [duplicate]

I know that the active class has to be managed manually in bootstrap. I have done that a few times with jQuery.

My newest project works with react though.

I know that I am not supposed to set classes in the dom with jQuery when using react. But how would this be done correctly?

like image 571
Alex Avatar asked May 21 '15 14:05

Alex


People also ask

How do I set bootstrap active class to NAV menu?

To set the active class to the navigation menu dynamically by scrolling or clicking on the navigation links, the active class is to be set on each section depending on the position of the webpage. To add methods and variables, JavaScript is used.

How do you change the background color of the active NAV item in react JS?

How do you change the background color of the active nav item React? The state of list of items can be changed by changing the background-color CSS property. Syntax: background-color: color | transparent; Property Values: color: It specifies the background color of element.


2 Answers

You simply have to specify the active value of the tabs in your render function. To know whether or not a tab should have a value for the the className property is to store it somewhere.

How do you store something in a react component? You use state. You haven't shown any code but you could simply keep track of which tab is currently active in your state.

You could for example have this:

getInitialState: function() {
    return { activeTabClassName: "tab1" };
}

render: function() {
    return (
        <ul>
            <li className={(this.state.activeTabClassName === "tab1") ? "active" : ""}></li>
            <li className={(this.state.activeTabClassName === "tab2") ? "active" : ""}></li>className            </ul>
    );
}

Warning: This code block is just one example and isn't tested. (There are several ways to do it).

You could also check this: Switch class on tabs with React.js

like image 55
Jeremy D Avatar answered Oct 03 '22 19:10

Jeremy D


You could use react-router to automatically or manually manage active state in bootstrap navbar.

like image 25
Phi Nguyen Avatar answered Oct 03 '22 17:10

Phi Nguyen