So I have the a react state as follows
[ state, setState] = useState(new Set([1,2,3]))
I want to add a bunch of elements to the set immutably, how can I do that? It is simple with Arrays and can be just done using setState(prev=>[...prev,...newElem]) but how do I do it in a set.
const newElems = new Set([4,5,6]) //These are new Elements that need to be appended
setState(prev=>new Set([..prev,...newElems])) //This can be done and works but how do without converting Array to Set
The above implementation can be used, but how can it be done without the overhead of reassigning an Array to a Set every time. Can I just not append it somehow (immutably)
P.S This question is different from the one in the link below as it is about adding multiple elements not a single one. Also it uses Hooks approach in React vs Class based approach, sure they are interchangeable, but this question is more up to date and might help others Using a Set data structure in React's state