How to destructure properly?
Hey community!
I'm brand new to SolidJS so I apologize if this is a basic question. I'm trying to find a way to destructure props the SolidJS way. I've tried this a couple of different ways, and I seem to run into problems. My issue stems from a request that returns a bunch of nested data (GraphQL), and I'd like to break that data up into pieces.
I'd like to change data that looks like this:
data()?.user?.repositoriesContributedTo.nodes
17 Replies
hi creme 👋 could u elaborate on what's not working as u expected?
Oh sorry, I totally spaced on including the errors I get:
Property 'user' does not exist on type 'DeepSpread<ReactiveSource>'
is it only a type-error or does it errors during runtime?
There is one more error in browser:
Uncaught TypeError: can't convert undefined to object
destructure index.js:39
And this is what I'm passing to destructure:
const data: Resource<UserReposQuery | undefined>
mm ye my interpretation (full disclosure: never used
destructure
) is that destructure
allows for undefined
(see https://primitives.solidjs.community/package/destructure#accessing-keys-initially-missing) but only at the leavesSolid Primitives
A library of high-quality primitives that extend SolidJS reactivity
this would error in regular js too
I really appreciate your responses by the way
thank u!
Ah yes, I think that error makes sense. Can't we circumvent with:
the above would still error in js
have you looked at
reconcile
and solid's store
?Gotcha. So what would the solution be? Just destructure user and then access its properties with optional chaining? Would it be bad practice to user destructure again on
user
?
Yes to store, no to reconcile. Though I think my understanding of store is poorI tend to use this approach:
you can do it manually, like peer proposes
Ahhh, that does exactly what I want it to do.
I would personally just do
but it's the same idea
Yep, will go with the second proposed approach.
You guys are awesome. Thanks for answering this, and so fast also.
the idea of reconcile is to take immutable data (like a response from a fetch) and then diff it and only update the properties that have changed (aka finegrain it)
but it might be a bit of a hassle to combine it with
createResource
appreciate 🙏