API reference
JSX runtime
Section titled “JSX runtime”-
jsx,jsxs,jsxDEV— automatic JSX runtime functions. You normally do not call these directly; configure your bundler to usedomstatejsxas the JSX import source (see Getting started). -
createElement,Fragment— classic JSX runtime. Used with pragma comments:/** @jsx createElement *//** @jsxFrag Fragment */ -
Fragment— renders a nativeDocumentFragment. Fragments are transparent in the DOM: their children are moved into the parent element. Refs and context Providers cannot be attached to fragment-returning components.
-
useRefs()— returns an endless list of empty ref objects.const [a, b, c] = [{}, {}, {}]; // equivalent to useRefs() -
useRefProxy()— returns an object that lazily creates refs when you access properties on it.const refs = useRefProxy();// refs.username creates a ref on first access
Hooks take a ref and return a [get, set] pair. Setters accept either a value
or a function (prev) => next. See Hooks for examples.
useTextContent(ref)— read/write text contentuseIntContent(ref)— read/write text content as an integeruseTextInput(ref)— read/write the value of a text inputuseNumberInput(ref)— read/write the value of a number inputuseCheckbox(ref)— read/write thecheckedstatus of a checkboxuseStyleBoolean(ref, property, onValue, offValue)— toggle a style propertyuseClassBoolean(ref, onValue, offValue)— toggle classesusePropertyBoolean(ref, property, onValue, offValue)— toggle an HTML propertyuseErrorMessage(ref)— read/write text content, hiding the element when falsyuseList(ref, component)— append children created from a componentuseLocalStorage(key)— read/write alocalStoragevalueuseControlledInput(ref)— treat a custom component as a controlled inputcombineHooks(...hooks)— combine several hooks into one[get, set]pair
Context
Section titled “Context”See Contexts for a full guide.
createContext(defaultValue?)— create a new context objectContext.Provider— attach a value to the DOM so it can be looked up; must wrap a single elementuseContextUp(node, context)— walk up fromnodeto find a provideruseContextDown(node, context)— search the subtree undernodefor providersuseContextSide(node, context, upContext)— find a common ancestor’s subtree, then search downwardsfindUp(node, context)— likeuseContextUpbut returns the provider element
Data fetching
Section titled “Data fetching”See Queries for examples.
useQuery(options)— fetch remote data; returns{ refetch }useMutation(options)— mutate remote data; returns{ mutate }
See Forms for a full guide.
useForm(options)— returns{ registerForm, register, registerError, registerButton, handleSubmit, getData, reset }
Routing
Section titled “Routing”See Routing for a full guide.
Route— a component that renders its children when its path matchesLink— a component that renders a button that navigates to a path
Other utilities
Section titled “Other utilities”runOnlyLast(fn)— ensure only the most recent invocation offnruns.