I have a useState object of:
const [filterState,setFilterState] = useState({
Brands:{
NewBalance: false,
Nike: false,
Addiddas: false,
Converse:false,
UnderArmour:false,
},
Gender:{
Male: false,
Female: false,
Kids:false,
}
});
and i am rendering each brand as a checkbox like this:
{
Object.entries(filterState.Brands).map(([brand, checked])=>
<div className="" key={brand} >
<label htmlFor={brand} className="mr-[10px]">{brand}: </label>
<input type="checkbox" name={brand} className="cursor-pointer" checked={checked}
onChange={()=>setFilterState({Brands:{...filterState.Brands,Nike:!checked},Gender:
{...filterState.Gender}})}/>
</div>
)}
If you look at the onChange that’s where I have the problem:
onChange={()=>setFilterState({Brands{...filterState.Brands,Nike:!checked},Gender{...filterState.Gender}})}/>
, if I give it an exact key like Nike
then I have no issue but that only changes one checkbox. If I change the Nike
to brand
then I get an error of: 'brand' does not exist in type ...
How do I make this work as brand is technically a string so it doesn’t work, how do I make it check/uncheck the correct boxes, thanks
3
Answers
Your object is not really TS friendly, try changing to the following object style.
Then you can declare your type as something like
Then you no longer need to use
Object.entries
, you can use basic maps and filtercodesandbox
You have a syntax error in your onChange. Should be:
A bit more legibly, that’s:
The typing of
Object.entries
can be improved.TS Playground