useTransition
For more detailed information about React concepts and APIs, please refer to the official documentation for useTransition.
useTransition lets you update state without blocking the UI. State updates inside startTransition are marked as non-urgent: React keeps showing the current UI, renders the new UI in the background and can interrupt that render for more urgent updates such as clicks or typing.
It returns isPending, which is true while the transition is rendering, and the startTransition function.
let isPending, startTransition = React.useTransition ()
Html.button [
prop.onClick (fun _ -> startTransition (fun () -> setTab "posts"))
prop.text (if isPending then "Loading..." else "Posts")
]
In the example below, rendering the posts tab takes about 300 ms. Click "posts" and then quickly "contact": the buttons stay responsive and the slow render is abandoned.
Click 'posts' and then quickly 'contact': the slow render is interrupted.
Show code
module Example.UseTransition
open Fable.Core
open Feliz
/// Takes about 1 ms to render, so a list of 300 of them is noticeably slow.
[<ReactComponent>]
let SlowPost (index: int) =
let start = JS.Constructors.Date.now ()
while JS.Constructors.Date.now () - start < 1.0 do
()
Html.text $"Post #{index}"
/// Memoized, so re-renders of the parent (e.g. showing the pending state) skip it.
[<ReactMemoComponent>]
let PostsTab () =
Html.ul [
for index in 1..300 -> Html.li [ prop.key index; prop.children [ SlowPost index ] ]
]
[<ReactComponent(true)>]
let UseTransition () =
let tab, setTab = React.useState "about"
let isPending, startTransition = React.useTransition ()
Html.div [
Html.div [
for name in [ "about"; "posts"; "contact" ] ->
let isActive = tab = name
Html.button [
prop.key name
prop.style [ style.marginRight 8 ]
prop.disabled isActive
// Rendering the posts tab is slow. As a transition it doesn't block the buttons.
prop.onClick (fun _ -> startTransition (fun () -> setTab name))
prop.text name
]
]
if isPending then
Html.p [ prop.style [ style.color "gray" ]; prop.text "Loading..." ]
match tab with
| "posts" -> PostsTab()
| "contact" -> Html.p "You can find us on GitHub."
| _ -> Html.p "Click 'posts' and then quickly 'contact': the slow render is interrupted."
]
Async transitions with useAsyncTransition
Since React 19, a transition can be async: isPending stays true until the returned promise settles. React calls such async transitions "Actions".
The returned startTransition function can only have one F# type, so Feliz offers a second hook for async work: React.useAsyncTransition is the same React hook, but its startTransition takes a unit -> JS.Promise<unit>.
let isPending, startTransition = React.useAsyncTransition ()
startTransition (fun () -> promise {
let! saved = saveName name
React.startTransition (fun () -> setSavedName saved)
})
State updates after a let! or do! are no longer part of the transition. Wrap them in React.startTransition again, as shown above. This is a current limitation of React, not of Feliz.
There is no Async<'T> overload: React only waits for promises. Convert an F# async with Async.StartAsPromise.
Saved name: nobody
Show code
module Example.UseAsyncTransition
open Fable.Core
open Feliz
/// Pretends to save the name on a server.
let saveName (name: string) : JS.Promise<string> = promise {
do! Promise.sleep 1500
return name.Trim()
}
[<ReactComponent(true)>]
let UseAsyncTransition () =
let name, setName = React.useState ""
let savedName, setSavedName = React.useState "nobody"
let isPending, startTransition = React.useAsyncTransition ()
Html.div [
Html.input [ prop.value name; prop.onChange setName; prop.placeholder "Your name" ]
Html.button [
prop.disabled (isPending || name = "")
prop.onClick (fun _ ->
startTransition (fun () -> promise {
let! saved = saveName name
// Updates after `let!` are not part of the transition anymore, so wrap them again.
React.startTransition (fun () -> setSavedName saved)
})
)
prop.text (if isPending then "Saving..." else "Save")
]
Html.p $"Saved name: {savedName}"
]
See also
startTransitionto start a transition outside of a component, withoutisPending.useDeferredValueto defer a value instead of a state update.useActionStatefor async actions triggered by forms.