This documentation is available as Markdown for AI agents and LLMs. See the full Markdown index or append .md to any documentation URL.

useNativeState

一个 React 钩子,用于创建在 JavaScript 和原生 SwiftUI 视图之间共享的可观察状态。

iOS
tvOS
Included in Expo Go
Recommended version:
~58.0.15

useNativeState 返回一个 ObservableState,它在原生端映射到 SwiftUI 的 ObservableObject,因此对 .value 的读取和写入会被 SwiftUI 直接观察,而无需经过 React 渲染周期。这使你可以从 UI 线程上的工作单元同步更新原生视图。

安装

🌐 Installation

Terminal
- npx expo install @expo/ui
- yarn expo install @expo/ui
- pnpm expo install @expo/ui
- bun expo install @expo/ui

If you are installing this in an existing React Native app, make sure to install expo in your project.

用法

🌐 Usage

下面的示例在用户输入时会对电话号码进行屏蔽。格式化和对 maskedPhone.value(文本)以及 selection.value(光标位置)的写入都在 UI 线程上同步进行,因此在输入值和屏蔽值之间不会出现闪烁。

🌐 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.

WorkletPhoneMaskExample.tsx
import { Host, TextField, useNativeState } from '@expo/ui/swift-ui'; import { keyboardType } from '@expo/ui/swift-ui/modifiers'; import { useCallback } from 'react'; export default function WorkletPhoneMaskExample() { const maskedPhone = useNativeState(''); const selection = useNativeState({ start: 0, end: 0 }); const handleTextChange = useCallback( (v: string) => { 'worklet'; const digits = v.replace(/\D/g, '').slice(0, 10); let formatted: string; if (digits.length === 0) { formatted = ''; } else if (digits.length <= 3) { formatted = digits; } else if (digits.length <= 6) { formatted = `(${digits.slice(0, 3)}) ${digits.slice(3)}`; } else { formatted = `(${digits.slice(0, 3)}) ${digits.slice(3, 6)}-${digits.slice(6)}`; } if (formatted !== v) { maskedPhone.value = formatted; // Snaps to end for demo. Real masks need smarter cursor handling. selection.value = { start: formatted.length, end: formatted.length, }; } }, [maskedPhone, selection] ); return ( <Host matchContents> <TextField text={maskedPhone} selection={selection} placeholder="(555) 123-4567" modifiers={[keyboardType('phone-pad')]} onTextChange={handleTextChange} /> </Host> ); }

Worklet 回调

🌐 Worklet callbacks

报告变化的修饰符,比如 onHingeChange,可以使用普通函数(在 JavaScript 线程上运行)或者 useWorkletCallback 的回调(在 UI 线程上同步运行)。把它和 useNativeState 搭配使用,就能在每次变化时更新视图,而无需 React 重新渲染。

🌐 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.

WorkletHingeExample.tsx
import { Host, Text, useNativeState, useWorkletCallback, VStack, } from '@expo/ui/swift-ui'; import { onHingeChange } from '@expo/ui/swift-ui/modifiers'; export default function WorkletHingeExample() { const isFullyOpen = useNativeState(false); const onHinge = useWorkletCallback((_, newContext) => { 'worklet'; isFullyOpen.value = newContext.hinge?.status === 'fullyOpen'; }); return ( <Host matchContents> <VStack modifiers={[onHingeChange(onHinge)]}> <Text>Fold the device</Text> </VStack> </Host> ); }

应用接口

🌐 API

import { useNativeState } from '@expo/ui/swift-ui';

No API data file found, sorry!