I know there is various solutions for this question already on SO.But I can't make it work.
const [isActive, setActive] = useState(false);
const toggleClass = () => {
setActive(!isActive);
};
<ul>
<li>className="nav-item "><a href="#!"
className={isActive ? 'm-active nav-link': "nav-link"}
onClick={toggleClass}
data-toggle="tab"
id=1>POSTS</a>
</li>
<li>........className={isActive ? 'm-active nav-link': "nav-link"}
onClick={toggleClass}.............................</li>
<li>........className={isActive ? 'm-active nav-link': "nav-link"}
onClick={toggleClass}.............................</li>
<li>........className={isActive ? 'm-active nav-link': "nav-link"}
onClick={toggleClass}.............................</li>
</ul>
So,right now when I click any li item ,it selects and add other li items to m-active className What this looks like you can see here.enter image description here
This is happening because it doesn't know which li to select .so, I thought to add id=1 , id=2, id=3and id =4 in all li tags and then pass that value with fuction and only add m-active class only to that li tag which has same id but I don't know how can I target only that li which have same id passed by the function
const toggleClass = (id) => {
setActive(!isActive);
};
<li>className="nav-item "><a href="#!"
className={isActive ? 'm-active nav-link': "nav-link"}
onClick={() => toggleClass(1)}
data-toggle="tab"
id=1>POSTS</a>
Please see if you can help me with this or if you have other idea to solve this problem