如何在不使用诸如onChange之类的监听器的情况下获取组件的元素?

huangapple go评论70阅读模式
英文:

How to get element of component without using listener such as onChange?

问题

I would presumably like to run the function auto_grow on useEffect but i require the element to be passed in to edit its values. Is there a way I can pass in the element without a listener so useEffect can make use of it?

I tried document.getElementById but that returns just the textarea and scrollHeight isnt a property.

Code:

function ChatInput(props){
    const [text, setText]=useState();

    function auto_grow(element){
        element.target.style.height = "5px";
        console.log(element)
        element.target.style.height = (element.target.scrollHeight) + "px";
    }

    useEffect(() => {
        setText(props.answer)
    }, [props.answer])

    return (
        <>
            <Container style={{paddingTop: '.5rem'}}>
                <InputGroup>
                    <InputGroup.Text>Bot</InputGroup.Text>
                    <Form.Control onLoad={auto_grow} style={textareaStyle} as="textarea" value={text} aria-label="Chat Input"/>
                </InputGroup>
            </Container>
        </>
    )
}

const textareaStyle = {
    resize: "none",
    overflow: "hidden",
    minHeight: "50px",
    maxHeight: "1000px"
}

export default ChatInput;
英文:

I would presumably like to run the function auto_grow on useEffect but i require the element to be passed in to edit its values. Is there a way I can pass in the element without a listener so useEffect can make use of it?

I tried document.getElementById but that returns just the textarea and scrollHeight isnt a property.

Code:

function ChatInput(props){
    const [text, setText]=useState();

    function auto_grow(element){
        element.target.style.height = &quot;5px&quot;;
        console.log(element)
        element.target.style.height =(element.target.scrollHeight)+&quot;px&quot;;
        
        
    }

    useEffect(()=&gt;{
        setText(props.answer)
        
    },[props.answer])
        return(
            &lt;&gt;
            &lt;Container style={{paddingTop:&#39;.5rem&#39;}}&gt;
                &lt;InputGroup&gt;
                    &lt;InputGroup.Text&gt;Bot&lt;/InputGroup.Text&gt;
                    &lt;Form.Control onLoad={auto_grow}style={textareaStyle} as=&quot;textarea&quot; value={text} aria-label=&quot;Chat Input&quot;/&gt;
                &lt;/InputGroup&gt;
            &lt;/Container&gt;
            &lt;/&gt;
        )
    
}

const textareaStyle = {
    resize: &quot;none&quot;,
    overflow: &quot;hidden&quot;,
    minHeight: &quot;50px&quot;,
    maxHeight: &quot;1000px&quot;
}



export default ChatInput;

答案1

得分: 2

你可以使用 ref 来获取底层元素。

import { useState, useEffect, useRef } from 'react';
// ...
function ChatInput(props) {
    const ref = useRef();
    // ref 使用示例:
    useEffect(() => {
        ref.current.style.height = ref.current.scrollHeight + 'px';
    }, []);
    // ...
    return (
        // 其他元素...
        <Form.Control ref={ref} as="textarea" value={text} aria-label="Chat Input" />
    );
}
英文:

You can use a ref to get the underlying element.

import { useState, useEffect, useRef } from &#39;react&#39;;
// ...
function ChatInput(props) {
    const ref = useRef();
    // ref usage example:
    useEffect(() =&gt; {
        ref.current.style.height = ref.current.scrollHeight + &#39;px&#39;;
    }, []);
    // ...
    return (
        // other elements...
        &lt;Form.Control ref={ref} as=&quot;textarea&quot; value={text} aria-label=&quot;Chat Input&quot; /&gt;
    );
}

</details>



# 答案2
**得分**: 1

你错了`scrollHeight` 是每个 DOM 元素的属性

```javascript
var element = document.getElementById('test')
console.log(element.scrollHeight)
<body>
  <div id="test">xxx</div>
</body>
英文:

you are mistaken. scrollHeight is a property of each dom element

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

var element = document.getElementById(&#39;test&#39;)
console.log(element.scrollHeight)

<!-- language: lang-html -->

&lt;body&gt;
&lt;div id = &quot;test&quot;&gt;xxx&lt;/div&gt;
&lt;/body&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年6月1日 07:04:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/76377770.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定