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?
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 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.
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
You could use react-router to automatically or manually manage active state in bootstrap navbar.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With