useFormStatus
Check out the official React docs!
For more detailed information about React concepts and APIs, please refer to the official documentation for useFormStatus.
ReactDOM.useFormStatus returns the status of the last submission of the parent form. It must be called from a component rendered inside an Html.form, which makes it handy for reusable submit buttons that don't need any props.
The returned FormStatus has:
| Field | Meaning |
|---|---|
pending | true while the parent form is submitting. |
data | The submitted FormData, or None when there is no active submission. |
method | "get" or "post". |
action | The function or URL passed to the parent form's prop.action. |
[<ReactComponent>]
let SubmitButton () =
let status = ReactDOM.useFormStatus ()
Html.button [ prop.type'.submit; prop.disabled status.pending; prop.text "Send" ]
pending only becomes true for form actions (prop.action with a function), not for forms handled with prop.onSubmit.
Show code
module Example.UseFormStatus
open Fable.Core
open Browser.Types
open Feliz
/// Reads the status of the form it is rendered in; no props needed.
[<ReactComponent>]
let SubmitButton () =
let status = ReactDOM.useFormStatus ()
Html.button [
prop.type'.submit
prop.disabled status.pending
prop.text (
match status.data with
| Some data when status.pending ->
let message = unbox<string> (data.get "message")
$"Sending '{message}'..."
| _ -> "Send"
)
]
[<ReactComponent(true)>]
let UseFormStatus () =
let lastMessage, setLastMessage = React.useState ""
Html.form [
prop.action (fun (formData: FormData) -> promise {
do! Promise.sleep 1500
React.startTransition (fun () -> setLastMessage (unbox<string> (formData.get "message")))
})
prop.children [
Html.input [ prop.name "message"; prop.placeholder "Message" ]
SubmitButton()
if lastMessage <> "" then
Html.p $"Sent: {lastMessage}"
]
]