This documentation is available as Markdown for AI agents and LLMs. See the full Markdown index or append .md to any documentation URL.
This is documentation for the next SDK version. For up-to-date documentation, see the latest version (SDK 58).
useNativeState
A React hook that creates observable state shared between JavaScript and native SwiftUI views.
useNativeState returns an ObservableState that maps to a SwiftUI ObservableObject on the native side, so reads and writes to .value are observed directly by SwiftUI without going through the React render cycle. This lets you update the native view synchronously from a worklet on the UI thread.
Note: When working with the React Compiler, you should refrain from accessing and modifying the
valueproperty directly. Instead, use thegetandsetmethods, which are compliant with the React Compiler standards.
Installation
If you are installing this in an existing React Native app, make sure to install expo in your project.
Usage
Note: Using worklets requires installing
react-native-workletsin your project.useNativeStateitself works without it, but the synchronous UI-thread updates shown below depend on the worklet runtime.
The example below masks a phone number as the user types. The formatting and the writes to maskedPhone.value (text) and selection.value (cursor position) all happen synchronously on the UI thread, so there is no flicker between the typed value and the masked value.
Worklet callbacks
Modifiers that report changes, such as onHingeChange, take either a plain function, which runs on the JavaScript thread, or a callback from useWorkletCallback, which runs synchronously on the UI thread. Pair it with useNativeState to update a view on every change without a React render.
API
import { useNativeState } from '@expo/ui/swift-ui';
Hooks
Creates an observable native state that is automatically cleaned up when the
component unmounts. initialValue is captured once on the first render
ObservableState<T>Wraps a worklet function so native code can run it synchronously on the UI thread, with no
JS-thread round trip. Pass the result to a modifier that accepts worklet callbacks, such as
onHingeChange. The callback must start with the 'worklet' directive, and
react-native-worklets must be installed.
The native callback is updated when callback changes and released when the component unmounts,
so call it at the top of your component like any hook.
WorkletCallback<T>Example
const angle = useNativeState(180); const onHinge = useWorkletCallback((_, newContext) => { 'worklet'; angle.value = newContext.hinge?.angle ?? 180; }); <VStack modifiers={[onHingeChange(onHinge)]} />
Types
Observable state shared between JavaScript and native views (Jetpack Compose on Android and SwiftUI on iOS).
Type: SharedObject extended by:
A worklet function wrapped for native code, returned by useWorkletCallback.
Pass it to a modifier or prop that accepts worklet callbacks, which then runs it synchronously on
the UI thread.
Type: SharedObject extended by: