Item in store array being overwritten

I have created the following store: { elements: Element[], selectedElement: Element } . When an element from the array is clicked it is assigned to selectedElement. If a new item is selected and assigned to selectedElement, it actually overwrites the value of the previously selected element in the array. Is this the expected behavior?
4 Replies
oneiro
oneiro8mo ago
Could you provide a bit more detail, please? How are you setting selectedElement?
tehquickness
tehquickness8mo ago
setState("selectedElement", elem); When a click happens I find the clicked element from state.elements, then I use the setState above Interestingly enough, if I do this: batch(() => { setState("selectedElement", null); setState("selectedElement", elem); }), It seems to work
REEEEE
REEEEE8mo ago
I think you'll have to spread elem
tehquickness
tehquickness8mo ago
Spread operator does indeed fix it! My guess is that elem is a proxy object so when it gets assigned to selectedElement which was also a proxy object, it tries to replace in the array maybe It feels kind of like pointers. If I take a proxy object and assign it directly to selectedElement, the next next selectedElement is assigned , it overwrites the original element still in the array. Assigning null clear clears the assigned element or using spread creates a brand new object