Worklet integration in Expo UI: synchronously controlling SwiftUI and Compose state
Key Points
- Synchronous UI-thread callbacks
- Shared native state for SwiftUI and Compose
- Flicker-free input masking on keystrokes
Summary
SDK 56 adds first-class UI runtime worklet support to Expo UI (via react-native-worklets). You can run event callbacks synchronously on the native UI thread and use useNativeState to create a shared native state observed by both SwiftUI and Jetpack Compose. That lets TextInput callbacks execute and update native state without ever hopping to the JS thread, enabling low-latency interactions like flicker-free input masking.
Key Points
- API surface: useNativeState, Host, TextInput; worklet callbacks run on the UI thread (use the 'worklet' directive).
- Native mapping: useNativeState becomes an ObservableObject on iOS and a MutableState on Android; worklets execute directly when the native view fires events.
- Immediate benefits: synchronous updates and re-renders on keystroke—e.g., you can rewrite text.value in the same frame to implement flicker-free credit-card, phone, date, or currency masking.
- Requirements and scope: landed in SDK 56; requires react-native-reanimated and react-native-worklets; available in @expo/ui/swift-ui and @expo/ui/jetpack-compose. TextInput is one of the first components wired; more form controls will follow.
- Guidance for engineers: prefer worklets for low-latency UI tasks and masking; avoid heavy computation on the UI thread and keep complex business logic on JS/native background threads.