Adapting a list of objects to signals
I've got an API that returns a list of objects like
I fetch this data using
useResource
and I render it out with a tree of components like
The problem comes when I invalidate the resource and re-fetch it, all of the Result
components get re-rendered from scratch, because the objects are different. And even if the objects were the "same" then they wouldn't update properly because props.result.value
isn't a signal.
Does solid provide any tools for mapping data like this into signals so that my components can be reactive to it?5 Replies
mapArray
isn't the right solution, since there's no way to provide it with a surrogate key
I've written my own remap<U, V>(input: Iterable<U>, target: V[], { key: (i: U) => unknown, create: (i: U) => V, update: (i: U, e: V) => V, destroy: (e: V) => void })
but it's annoyingly complex
and doesn't currently handle reordering wellYou can take a look at the storage option for resources and make use of the createDeepSignal example provided in the docs
I think you are looking for the storage option on createResource which usually is implemented using reconcile.
The simplest way to make it not rerender from scratch is to use
<Index>
instead of <For>
, but if you need them keyed by something other than index there are other community primitives / components that you can specify your own key, like <Key>
https://github.com/solidjs-community/solid-primitives/tree/main/packages/keyed#readmeGitHub
solid-primitives/packages/keyed at main · solidjs-community/solid-p...
A library of high-quality primitives that extend SolidJS reactivity. - solidjs-community/solid-primitives
Also if you want it keyed by reference, like
<For>
, but also don't want it to reattach everything when everything changes, like <Index>
avoids that, there also <List>
(I did that actually), it's something in between these two, but it's pretty new https://github.com/solidjs-community/solid-primitives/tree/main/packages/list#readmeGitHub
solid-primitives/packages/list at main · solidjs-community/solid-pr...
A library of high-quality primitives that extend SolidJS reactivity. - solidjs-community/solid-primitives