admin管理员组

文章数量:1393049

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>
  );
};

本文标签: