react 函数组件使用useRef获取input框中的值

407 阅读1分钟


import React, { useRef } from "react";

export default function App() {
    const inputVal = useRef(null)
    const inputValOpt = () => {
        console.log(inputVal.current.value)
    }
    return (
        <div>
            <input type='text' ref={inputVal} />
            <button onClick={inputValOpt}>点击</button>
        </div>
    )
}