Skip to main content

createRoot

Check out the official React docs!

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


ReactDOM.createRoot is used to initialize a React root and render your application into a DOM element. This is the recommended way to start a React app in modern React.

module Example.CreateRoot

open Feliz
open Fable.Core.JsInterop


[<ReactComponent>]
let private App () = Html.h1 "Hello, Feliz!"

let private root = ReactDOM.createRoot (Browser.Dom.document.getElementById "root")
root.render (App())

Root options​

Pass RootOptions as the second argument to hook into React's error handling, or to prefix the IDs generated by React.useId:

OptionMeaning
onCaughtErrorCalled when an error boundary catches an error.
onUncaughtErrorCalled when an error is not caught by any error boundary. React unmounts the app in that case.
onRecoverableErrorCalled when React recovers from an error by itself, e.g. a hydration mismatch.
identifierPrefixPrefix for useId. Avoids ID conflicts when several React roots share a page.

The error callbacks receive the error and a ReactErrorInfo with the component stack. They are a good place to report errors to a logging service.

module Example.CreateRootOptions

open Browser
open Feliz

[<ReactComponent>]
let private App () = Html.h1 "Hello, Feliz!"

let private root =
ReactDOM.createRoot (
document.getElementById "root",
RootOptions(
// Prefix for IDs from React.useId, e.g. when several React roots share a page.
identifierPrefix = "app-",
// An error boundary caught an error (it already rendered its fallback).
onCaughtError = (fun error info -> console.warn ("Caught:", error.Message, info.componentStack)),
// No error boundary caught the error; React unmounted the app. Report it.
onUncaughtError = (fun error info -> console.error ("Uncaught:", error.Message, info.componentStack)),
// React recovered on its own, e.g. after a hydration mismatch.
onRecoverableError = (fun error _ -> console.warn ("Recovered:", error.Message))
)
)

root.render (App())