🌐 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';exportdefaultfunctionWorkletPhoneMaskExample(){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 ='';}elseif(digits.length<=3){
formatted = digits;}elseif(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(<HostmatchContents><TextFieldtext={maskedPhone}selection={selection}placeholder="(555) 123-4567"modifiers={[keyboardType('phone-pad')]}onTextChange={handleTextChange}/></Host>);}
🌐 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.