JavaScript/react/useRef:修订间差异
来自康健生活
< JavaScript | react
| 第9行: | 第9行: | ||
<syntaxhighlight lang="html5"> | <syntaxhighlight lang="html5"> | ||
const baseRef = useRef<any>();</syntaxhighlight> | const baseRef = useRef<any>();</syntaxhighlight> | ||
=== 存储数组 === | |||
<syntaxhighlight lang="html5"> | |||
const [ inputIpDom, setinputIpDom ] = React.useState<any[]>([])</syntaxhighlight> | |||
2021年2月28日 (日) 09:30的版本
引言
useRef
引用
使用 useRef 可以绑定引用(类似绑定 this),将 dom 元素上的引用绑定到 useRef 上。如下,当前 div 元素上的引用被绑定到了 baseRef 上,baseRef 具有当前 div 元素的属性,如 interHTML(baseRef.current.innerHTML) 等。
<div style={{ width: 201 }} ref={baseRef}></div>
任何值
useRef 值会有类型限制,这时可以如下操作:
const baseRef = useRef<any>();
存储数组
const [ inputIpDom, setinputIpDom ] = React.useState<any[]>([])
No categories