How Do I Update Client Signals When Using "use server";
I've set up a Supabase AuthService that tracks the error state with a signal.
If I get an error from Supabase, I set the error state in my AuthService file. Here's an example from my Logout function:
Any pages that use the AuthService can import the
supabaseError
signal, and display error messages when supabaseError().length > 0
.
This works great when I do everything on the client.
BUT, I'm running into trouble when I have to use "use server";. If I use setSupabaseError() from the server, the DOM does not update.
For context, here's my action for deleting an account.
So, how can I update a client-side signal when using "use server";?
The docs state the following about Global state and the server:
"It is recommended that application state should always be provided via Context instead of relying on global."
So, do I need to use context?
Meta frameworks are new to me, so please excuse my ignorance 🙂
Thank you!
Chris17 Replies
Return the error message from the action and then use "useSubmission" to get the value
So return the error instead of throwing
or you can do the try-catch after calling the server action
You might be asking how to do it
@lxsmnsyc 🤖 🤖 and @brenelz I've spent the last hour trying to get your suggestions to work but I'm still stuck.
I tried to read about useSubmission, but the docs don't really help.
So I tried to return the error.message from the server and then handle the error on the client but I keep getting the following error:
Here's my attempt:
Here's how I'm calling the function on the
delete-account
page:
And here's my server function:
Any help would be appreciated.for the
$R
the upcoming patch release for Start would fix it. as for the the implementation of your server function, follow my previous message
The thing is, if your code has a mix of server and client logic, just split it, where the server logic is in a "use server" functionThank you. Let me try that on the client.
Does my server action look right to you?
for example
would be
Does my server action look right to you?No
🤦♂️ I'm sorry. I really am trying to solve this on my own without asking too many questions. I've literally spent all day on this.
@ChrisThornham this explains how to do this properly, which should solve most of your problem
When you say "wrappedServerStuff" does the "wrapped" part mean wrapped in an action?
Or can I avoid using an action altogether?
The point is if your logic has both server and client logic in it, you move your server logic to a separate function (and you could wrap it with
cache
or action
depending on the intent), and then turn that function into a server function with "use server"
this way, your original logic would remain "client-only" while having the ability to run server logic
here's probably a much more realistic example
The fix:
Ok... I rewrote everything, but it's still not working.
My client function:
My server function:
Now I'm getting:
Okay this looks a lot better now
Now it seems to have some server-side errors
what does the response on the client look like
From my jsx:
And I'm also getting:
this one is a common mistake.
supabaseError
seems to be a resource and so you need to check first if it's not undefined. Usual fix here is the optional chaining syntax
as for $R
, like I mentioned, upcoming patch release would add a fix for itSo the $R thing is a bug?
And yes, supabaseError is a signal.
Finally, thank you. I really appreciate your time. I'll keep plugging away at this.
@brenelz I've tried 100 combinations using @lxsmnsyc 🤖 suggestions, and it's not working. I've been at this for 6 hours.
I'm trying your approach with useSubmission and I'm getting an undefined error when I try to
console.log(deleting.result);
the result on the client.
Here's my simple example.
On my delete page in the client:
On the server:
Do you have any suggestions?
Again, I'm sorry for asking so many questions. I've taught myself, React, Solid, the majority of SolidStart, Go, Typescript, CSS etc from docs and tutorials but I keep running into error after error when using "use server" with SolidStart.@brenelz and @lxsmnsyc 🤖 I went back to the drawing board and straight to the examples in the docs.
I followed the "Returning from actions" example here: https://start.solidjs.com/core-concepts/actions#:~:text=Returning%20from%20actions
Everything works when I use the example as written. BUT, when I add "use server"; to the echo function, it breaks. Here's how I have it written.
The error occurs when calling myEcho in both places:
AND
Here's the error:
Here's the full component for clarity:
So, I'm lost at this point. I'm using SSR... maybe that's the problem? But that's kind of the point of SolidStart, isn't it?
NOTE: The docs appear to be wrong. The example calls echo instead of myEcho.
So...
AND
I made a commit on Github.
SolidStart Beta Documentation
SolidStart Beta Documentation
Early release documentation and resources for SolidStart Beta
Just noticed the docs are also contradicting themselves with
const myEcho = useAction(echo);
but in some cases myEcho
is used and others echo
is used directly.
I'll have to experiment with useAction. I've only used useSubmission, forms (with actions on POST) and createAsync.
In both the apps I'm currently using solidstart in, I'm following the pattern from the basic
template aka having all "use server"
directives in another file.
It just makes more sense to me, since you can't pass things from the client scope to it anyways.