(一)需求
最近在学习React,学到了React Hook 做了 useRef Demo。
(二)介绍
使用useRef
是为了修改某个DOM节点相关的操作。
下面的Demo是使用useRef
实现,input输入框获取焦点的例子。
/*
* @Author: ArdenZhao
* @Date: 2022-04-19 10:47:35
* @LastEditTime: 2022-04-19 10:54:38
* @FilePath: /react-ts/src/components/react/11-Hook-useRef.js
* @Description: file information
*/
import { useRef, useEffect } from 'react';
import { Input } from 'antd';
import "antd/dist/antd.css";
function HookUseRef(props) {
// 由useRef 声明一个变量
const inputRef = useRef(null);
useEffect(() => {
console.log('[ inputRef ] >', inputRef)
inputRef.current.focus();
}, []);
return (
<div>
<h1>Learn, {props.name}</h1>
<Input type="text" id="name" ref={inputRef} />
{/* <input type="text" ref={inputRef} /> */}
</div>
);
}
export default HookUseRef
写在最后的话
学习路上,常常会懈怠。
《有想学技术需要监督的同学嘛~》
https://mp.weixin.qq.com/s/Fy...
如果有需要的伙伴,可以加我微信:learningisconnecting
或者可以关注我的公众号:国星聊成长(我会分享成长的方法)
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。