Is there a better way of doing this sub and unsub? I fell like I am just translating React code xD
Hey! I was wondering id there would be a better way of handling this subscription on mount and unsubscription on unmount that I am not aware off. Overall for this piece of code I feel like I could be using more the Solid tools, maybe using a store instead of signals for the user and loading, but I will take a better looks at the docs/tutorial to see how I could change that part
20 Replies
Your onCleanup doesn't have to be outside the onMount. You could use a store with reconcile for the user but that's up to you. loading being a signal is fine.
Also, don't destructure props
Ohh, nice to know that, thanks!
Also, for the props destructuring I have just noticed that I did that when reading the code now
Probably did it without thinking because I was trying to ""translate"" some react code that I had here
But is there a reason for not doing that or just for convention?
Tyy, I wouldn't have noticed I could do it this way haha
Wouldn't using a store require me to access it with userStore.user every time? I did a version where a passed userStore.user as the "user" value in the provider but that made it so the Home.tsx didn't listen to updates in the user.
This what the code that didn't work:
It breaks reactivity when you destructure
The reason its not reactive, afaik, is because you're reading the
userStore.user
in the value prop of the context which isn't reactive. You could call the store user
and define all the properties of the store there. Then just pass the store to the context and you'll be able to do user.name
etc..Ohh, that makes sense!! Ty for the warning
Hey @lxsmnsyc 🤖, I got an "Unrecognized value. Skipped inserting" error and I saw you have answered some issues related to this on github
Can you tell what could be wrong in this code:
I think the issue is where you're using
user
you might be using user
in the jsx like <div> {user} </div>
, and that will throw a warning I believeoh, let me see
Oh, I just coppied and pasted that code and now it worked, maybe it was the incorrect use of
user
instead of user()
in the JSX somewhere else indeed
Also, now I kinda feel that using a store would be better @._rb, I would try to implement that
I started going this route of making the store have all properties of User but I realized stores can't be set to null or undefined, only objectsYou can do it the original way if you want, you just need to make the
user
property in the context value object to be a getterAnd I was following the route of checking if the user is logged in with
if (user)
, and if (user = {})
would be a true so that would work
You mean with a signal?
Something like this
ohh interesting
Is there a direct advantage of using a store instead of a signal for that?
Or like this if you rather do
user().name
Tbh right now I just feel that calling
user.email
would be more elegant that user().email
so thats why I am going that routeYou can do
user.email
hereNice, good idea
it's a JS getter not a function
Ohh, yeah nice, tyy
But is there?
Store vs signal advantage comes when you need to update each property individually and get reactivity for those properties.
A store just creates a signal for each property
The main reason I suggested a store was so that you can do
setUser(reconcile(user))
in the auth state change to only trigger changes on updated properties but I don't think it really matters for User
objects
so you can use a signal if you'd likeOhh, thats great, either way I choose to go, that really explains a lot, tysm