Ask Your Question
1

In what way can I increase the height of <TextInput> once the text wraps?

asked 2022-02-14 11:00:00 +0000

lakamha gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
1

answered 2021-08-23 23:00:00 +0000

plato gravatar image

You can use the onContentSizeChange prop of <TextInput> to handle the height increase.

Here's an example code snippet in React Native:

import React, { useState } from "react";
import { TextInput } from "react-native";

const MultilineInput = () => {
  const [height, setHeight] = useState(40);
  const [text, setText] = useState("");

  const handleContentSizeChange = (event) => {
    setHeight(event.nativeEvent.contentSize.height);
  };

  return (
    <TextInput
      multiline
      onContentSizeChange={handleContentSizeChange}
      style={{ height }}
      value={text}
      onChangeText={setText}
    />
  );
};

export default MultilineInput;

In this example, we use the useState hook to manage the height state and the text state of the input. We also define a handleContentSizeChange function that gets called whenever the size of the content inside the input changes.

We pass this function to the onContentSizeChange prop of the <TextInput> component. Inside the function, we set the new height of the input to the height of the content by accessing the nativeEvent property of the event object.

Finally, we set the height of the input as a style property with the height state variable.

With this implementation, the height of the input will increase automatically as the text wraps.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2022-02-14 11:00:00 +0000

Seen: 11 times

Last updated: Aug 23 '21