This documentation is available as Markdown for AI agents and LLMs. See the full Markdown index or append .md to any documentation URL.
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.
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>Types
Observable state shared between JavaScript and native views (Jetpack Compose on Android and SwiftUI on iOS).
Type: SharedObject extended by: