UndoTextInput.tsx (743B)
1 import * as React from 'react'; 2 import { TextInput } from 'react-native'; 3 4 type Props = React.ComponentProps<typeof TextInput>; 5 6 const UndoTextInput = (props: Props): JSX.Element => { 7 const { value, onChangeText, ...rest } = props; 8 const lastValue = React.useRef(value); 9 const textInput = React.useRef<TextInput>(null); 10 11 const handleChangeText = (textValue: string) => { 12 lastValue.current = textValue; 13 onChangeText(textValue); 14 }; 15 16 React.useEffect(() => { 17 if (value !== lastValue.current) { 18 textInput.current.setNativeProps({ text: value }); 19 lastValue.current = value; 20 } 21 }); 22 23 return ( 24 <TextInput {...rest} ref={textInput} onChangeText={handleChangeText} /> 25 ); 26 }; 27 28 export default UndoTextInput;