diff options
Diffstat (limited to 'bs5/universal/native/shared/ServerActionFromPropsClient.re')
-rw-r--r-- | bs5/universal/native/shared/ServerActionFromPropsClient.re | 30 |
1 files changed, 30 insertions, 0 deletions
diff --git a/bs5/universal/native/shared/ServerActionFromPropsClient.re b/bs5/universal/native/shared/ServerActionFromPropsClient.re new file mode 100644 index 0000000..a374870 --- /dev/null +++ b/bs5/universal/native/shared/ServerActionFromPropsClient.re @@ -0,0 +1,30 @@ +[@react.client.component] +let make = + ( + ~actionOnClick: + Runtime.server_function( + (~name: string, ~age: int) => Js.Promise.t(string), + ), + ) => { + let (isLoading, setIsLoading) = RR.useStateValue(false); + let (message, setMessage) = RR.useStateValue(""); + <div> + <button + className="font-mono border-2 py-1 px-2 rounded-lg bg-yellow-950 border-yellow-700 text-yellow-200 hover:bg-yellow-800" + onClick={_ => { + setIsLoading(true); + actionOnClick.call(~name="Lola", ~age=20) + |> Js.Promise.then_(response => { + setIsLoading(false); + Js.log(response); + setMessage(response); + Js.Promise.resolve(); + }) + |> ignore; + }}> + {React.string("Click me to get a message from the server")} + </button> + <div className="mb-4" /> + <div> <Text> {isLoading ? "Loading..." : message} </Text> </div> + </div>; +}; |