Skip to main content

Activity

Check out the official React docs!

For more detailed information about React concepts and APIs, please refer to the official documentation for Activity.


note

Requires React 19.2 or later.

React.Activity hides and shows part of the UI while keeping its state. With ActivityMode.Hidden, React hides the children with display: none, cleans up their effects and renders updates for them at low priority. Switching back to ActivityMode.Visible restores the previous state and re-runs the effects.

That makes it a good fit for tabs, sidebars or content you want to pre-render before the user navigates to it.

React.Activity(
(if isOpen then ActivityMode.Visible else ActivityMode.Hidden),
Sidebar()
)

In the example below, type into both text areas and hide them. The one inside Activity keeps its text; the one rendered with if is unmounted and loses it.

Inside Activity

Conditionally rendered

Show code
module Example.Activity

open Feliz

[<ReactComponent>]
let Draft (label: string) =
let text, setText = React.useState ""

Html.div [
Html.p label
Html.textarea [ prop.value text; prop.onChange setText; prop.placeholder "Type something" ]
]

[<ReactComponent(true)>]
let Activity () =
let showDrafts, setShowDrafts = React.useState true

let mode =
if showDrafts then
ActivityMode.Visible
else
ActivityMode.Hidden

Html.div [
Html.button [
prop.onClick (fun _ -> setShowDrafts (not showDrafts))
prop.text (if showDrafts then "Hide drafts" else "Show drafts")
]
// Hidden with Activity: the text is kept while hidden.
React.Activity(mode, Draft "Inside Activity")
// Hidden with `if`: the component unmounts and the text is lost.
if showDrafts then
Draft "Conditionally rendered"
]

React.Activity accepts a single child or a list of children.