Thanks for showing that it's possible to use Set instead of Array to handle React state. But what's the benefit of using this data structure? Checking item existence in a collection with has instead of includes seems like not a big deal.
Btw, I see a disadvantage of using Set instead of Array. If you need to render a list of selectedBrands, you'd need to convert it to array before iteration.
Thanks for your reply. I agree that Array would be enough for most use cases. But if you have to maintain a list in the state that requires constant insertion and removal of items Set would be a good use-case for that in my opinion.
Coming to your second question. I would not use Set to store values that need to rendered. Instead of that, it should be used to store the state which modifies the values we are rendering like the example I used in the article.
But if you have to maintain a list in the state that requires constant insertion and removal of items Set would be a good use-case for that in my opinion.
I really don't understand why Set is better in this case. Is it performance, because has is faster than includes?
I would not use Set to store values that need to rendered.
Well, it happened to me many times that project requirements change, and tomorrow you'll need to render values that today are stored in Set. Extra work to refactor Set to Array.
Hi, I am not saying Set should be the only thing used in this case. I am just saying it can be done using this way too. You are free to use whatever method suitable for your requirements.
With Set you don't have to implement duplication control manually, it provides it to you for free. If you add the same value for the second time, Set will just ignore it, while with Array you'll have the duplicate and you'll have to add more code to handle this case.
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
Thanks for showing that it's possible to use
Setinstead ofArrayto handle React state. But what's the benefit of using this data structure? Checking item existence in a collection withhasinstead ofincludesseems like not a big deal.Btw, I see a disadvantage of using
Setinstead ofArray. If you need to render a list ofselectedBrands, you'd need to convert it to array before iteration.Thanks for your reply. I agree that
Arraywould be enough for most use cases. But if you have to maintain a list in the state that requires constant insertion and removal of itemsSetwould be a good use-case for that in my opinion.Coming to your second question. I would not use
Setto store values that need to rendered. Instead of that, it should be used to store the state which modifies the values we are rendering like the example I used in the article.I really don't understand why
Setis better in this case. Is it performance, becausehasis faster thanincludes?Well, it happened to me many times that project requirements change, and tomorrow you'll need to render values that today are stored in
Set. Extra work to refactorSettoArray.Hi, I am not saying
Setshould be the only thing used in this case. I am just saying it can be done using this way too. You are free to use whatever method suitable for your requirements.With
Setyou don't have to implement duplication control manually, it provides it to you for free. If you add the same value for the second time,Setwill just ignore it, while withArrayyou'll have the duplicate and you'll have to add more code to handle this case.