React 组件的三大核心属性之 ref 在函数式组件中的基本使用

使用Hook可以在函数式组件中使用ref

import React, {useRef} from 'react'
import {Button} from "antd";


export default function FuncCompDemo(props) {

    //创建ref
    const refText = useRef()

    function showText() {
        alert(refText.current.value)
    }

    return (
        <div>
            <input type="text" ref = {refText}/>
            <button onClick={showText}>展示文本内容</button>
        </div>
    )

}
posted @ 2022-03-07 18:03  叕叕666  阅读(58)  评论(0)    收藏  举报