JavaScript/react/useRef:修订间差异
来自康健生活
| 第2行: | 第2行: | ||
== useRef == | == useRef == | ||
== 引用 == | == 引用 == | ||
使用 useRef 可以绑定引用(类似绑定 this),将 dom 元素上的引用绑定到 useRef | 使用 useRef 可以绑定引用(类似绑定 this),将 dom 元素上的引用绑定到 useRef 上。如下,当前 div 元素上的引用被绑定到了 baseRef 上,baseRef 具有当前 div 元素的属性 | ||
<syntaxhighlight lang="html5"> | <syntaxhighlight lang="html5"> | ||
<div style={{ width: 201 }} ref={baseRef}></div></syntaxhighlight> | <div style={{ width: 201 }} ref={baseRef}></div></syntaxhighlight> | ||