I'm still quite new to Typescript and react and unfortunately don't understand what I'm doing wrong. I have a tab style nav with a NavDropdown. Unfortunately the dropdown is the only tab that does not get the active class when one of the dropdown items is selected. I have already seen that some people had this problem and could solve it somehow but I can't find a way to do it in my case. Can anyone help me?
Currently my nav looks like this:
<Nav variant="tabs">
<Nav.Item>
<Nav.Link
onClick={(e: React.MouseEvent<Element, MouseEvent>) => setTab(e, "today")}
href="#today"
>
today
</Nav.Link>
</Nav.Item>
<NavDropdown
title={activeTab === "month" ? dateFormat(dateStart!, "MMMM yyyy") : "month"}
id="month"
>
{months.map((m) => (
<NavDropdown.Item onClick={() => setMonth(m)} key={dateFormat(m, "MMMM yyyy")}>
{dateFormat(m, "MMMM yyyy")}
</NavDropdown.Item>
))}
</NavDropdown>
I've modified the following parts to fix the tab navigation and month selection:
defaultActiveKey={activeTab}to theNavcomponent to activate the tab on loadNav.LinkaddedeventKey="today"to have the key for the today tab set up properlyNavDropdown.Itemaddedactive={m === month}wheremis the iterator of the map (e.g. Jan 2021, Feb 2021 etc) and month is the selected month. This is needed so the selection in the drop-down is working.onClickof the drop-down items addedsetMonth&setTabto update the state.NavDropdownaddedactive={activeTab === "month"}so the tab navigation is correctly updating.I've also used moment.js to generate the month array and to format the months. Generation of the array is from this SO answer.
You can have a look at the code below or in this Codesandbox.