useRef 返回一个可变的 ref 对象,其 .current 属性被初始化为传入的参数(initialValue)。返回的 ref 对象在组件的整个生命周期内保持不变。
示例代码:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<div id="box"></div>
<script crossorigin src="https://unpkg.com/react@16/umd/react.development.js"></script>
<script crossorigin src="https://unpkg.com/react-dom@16/umd/react-dom.development.js"></script>
<script src="https://cdn.bootcss.com/babel-standalone/7.0.0-beta.3/babel.js"></script>
<script type="text/babel">
const {useRef} = React
function Compont() {
const username = useRef()
const password = useRef()
const handleClick = () => {
console.log('用户名:' + username.current.value)
console.log('密码:' + password.current.value)
}
return(
<div>
用户名:<input type="text" ref={username} /> <br/>
密码:<input type="password" ref={password} /> <br/>
<buttonBfwOnClick={handleClick}>点击登入</button>
</div>
)
}
ReactDOM.render( <Compont/>, document.getElementById('box'))
</script>
</body>
</html>
网友回复
systemd-nspawn与unshare区别?
Firejail、Bubblewrap沙箱与docker、podman容器区别?
如何访问unshare -n -- python3 -m http.server 8000创建的网站url?
linux中如何限制进程执行非法的系统调用命令,例如reboot 删除等等?
pivot_root与chroot有啥区别?
linux中如何限制进程使用cpu、内存、网络的最大值?
用户态沙箱、硬件隔离、namespace + cgroups的隔离有啥不同?
OverlayFS、AUFS 、 Btrfs的区别是啥?
linux上各种Namespace有啥作用?
linux体积最小最精简的版本是多大?


