I am trying to implement autoresizing multiline TextInput.
I searched the web and asked several chat AIs and I got the following code snippet. It works when text is updated by a user with a keyboard.
But it does not work when text is updated programatically by using setText
function. How do I make it work as I intended?
import React, { useState } from 'react';
import { View, TextInput } from 'react-native';
const AutoResizingTextInput = () => {
const [text, setText] = useState('');
const [height, setHeight] = useState(40);
return (
<View>
<TextInput
style={{ height: Math.max(40, height) }}
multiline
onChangeText={setText}
value={text}
onContentSizeChange={(event) => {
setHeight(event.nativeEvent.contentSize.height);
}}
/>
</View>
);
};
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744745764a4591285.html
评论列表(0条)