Why does my react-aria custom grid list not update correctly #6211
-
I have written a custom grid list component When I use the list and want to dynamically update all the items it does not update correctly.
I'm using immer to produce a new copy of the entire list when the delete item button is pressed. This results in an entirely new formData object. But it seems react-stately is doing some caching or something because unless I copy each dashboard object in What is happening here? Is an old "version" of |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 1 reply
-
Does this page from the docs help? |
Beta Was this translation helpful? Give feedback.
-
This issue can occur due to the way React manages state. React enhances rendering performance by comparing previous and current states and updating only the changes. However, sometimes it may fail to properly compare states. Here, the problem arises because although the formData.dashboards array has been modified, React fails to detect it. This is because the produce function of Immer creates a new array, but the internal items maintain the same references. When React compares the differences between the previous and new states, it doesn't detect any changes in the references of the items. To solve this issue, when creating a new array with the produce function, the internal items should also be copied to new objects. Using the structuredClone function is one way to achieve this, but another approach is to leverage the built-in features of Immer. You can use Immer's draftToImmutable function to convert the modified objects into immutable objects.
By doing this, React will correctly detect the changed state and perform the update accordingly. |
Beta Was this translation helpful? Give feedback.
Does this page from the docs help?