How to avoid rerenders when using refetch ?
Hi, I'm trying to build a simple todo list app using Solid. Everything has been fine so far, I really enjoy using Solid!
When I decided to add CSS transitions when adding an item to the todo list, I noticed that all items in the list were re-rendered each time they were added. After some investigation, I found that this re-rendering was caused by
refetch
, which when called causes a re-rendering of the entire list. Using mutate
works as expected, so I suspect that the reactivity is lost because the list is replaced with a whole new object.
What's the best way to avoid this kind of re-renders?
4 Replies
The rerenders are likely due to the resource retriggering the nearest suspense boundary on refetch (which is, by default, at the root of your app). If you wrap where the resource is accessed in a
<Suspense>
then only that boundary will be triggered and the rest of your app won't be rerendered
I believe you can also use data.latest
instead of data()
to access the resource, then the latest value will always be displayed so the suspense boundary won't retriggerI already used a
Suspense
in my TodoList
component, but since the resource contains all the todos, the whole list is re-rendered on refetch.
I found reconcile
in the documentation which seems to be able to diff data changes and trigger renders only on what changed, can this be used with resources ?It might be easiest to use
.latest
then, as that means the latest data is always shown, even when you're refetching
.latest
doesn't trigger suspenses and only updates when the data changes (after a refetch for example)BTW to be clear, this is not technically a rerender of the component, but rather that the suspense boundary temporarily removes it from the DOM till the resource loads (but doesn't gets deleted and recreated)