Estoy tratando de hacer un cuadro de texto de expansión automática (hacia arriba) con JavaScript en una aplicación React. Tengo este error. También intenté usar window.getElementbyId en lugar de pero eso no funciona.
[![error image][1]][1] <ReactTextareaAutocomplete id="textbox" className="message-input" placeholder="Type your message.."/> const tx = document.getElementsByTagName("ReactTextareaAutocomplete"); const maxheight = 100; const normalheight = window.getComputedStyle(tx[0]).getPropertyValue('height').replace("px", ""); for (let i = 0; i < tx.length; i++) { tx[i].setAttribute("style", "margin-top:-3px;" + "height:" + (tx[i].scrollHeight > maxheight ? maxheight : tx[i].scrollHeight) + "px;overflow-y:hidden;"); tx[i].addEventListener("input", OnInput, false); } function OnInput() { this.style.marginTop = (normalheight - this.style.height.replace("px", "")) + "px"; this.style.height = "auto"; this.style.height = (this.scrollHeight > maxheight ? maxheight : this.scrollHeight) + "px"; } [1]: <https://i.stack.imgur.com/1UbRS.png>Implemente useRef() en su lugar para acceder al elemento.
Agregue el atributo ref en el elemento.
<ReactTextareaAutocomplete ref={rtaRef} />Y para la función:
const tx = useRef(null); Si tiene varias instancias del componente, use el bucle para crear una ref única usando el índice del bucle.