V
V
Vann Damm2019-10-22 18:12:19
JavaScript
Vann Damm, 2019-10-22 18:12:19

How to get textarea height?

How to get the height of an element's textarea (also get when it increases) in a React component. Thanks

Answer the question

In order to leave comments, you need to log in

4 answer(s)
K
Konstantin, 2019-10-22
@just_konstantin

1. Get a reference to the home element using ref
2. Get the height of the resulting DOM element

S
Shohruh Shaimardonov, 2019-10-22
@joeberetta

Height can be received through el.outerHeight
And to receive it when increases it is necessary to write the crutch. The most banal option is when you listen to the onchange event, you can also listen to the merge of the mouse (if you want to catch the resize with the mouse, and not the increase due to the amount of content)

K
KononovD, 2019-10-22
@KononovD

<textarea ref=(node=>this.__anyName) onInput={(e)=>console.log(e.currentTarget.scrollHeight)} />

and
const height = this.__anyName.scrollHeight

M
Mikhail Osher, 2019-10-22
@miraage

Working with textarea in React is not the most trivial.
You can see the implementations:
- TextareaAutosize in Material UI: https://github.com/mui-org/material-ui/blob/master...
- react-textarea-autosize: https://github.com/andreypopp/react -textarea-autosize

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question