当前位置:网站首页>Useref create dynamic reference
Useref create dynamic reference
2022-07-24 05:35:00 【Plum blossom three】
useRef Return to a variable ref object , Its .current Property is initialized to the parameter passed in (initialValue). Back to ref Objects persist throughout the life cycle of a component .
Daily development , May come across : Dynamic creation ref quote . For example, according to the background data , To dynamically generate html Elements , And binding events .
according to useRef The definition of , During initialization, parameter values can be defined as arrays or objects , Then dynamically add element references .
Example , Use ref={el => { domRefs.current[index] = el }} :
// Initialize to array
const domRefs = useRef([])
// Initialize as object
// const domRefs = useRef({})
import { useRef } from 'react'
const Index = () => {
const domRefs = useRef([])
return (
<ul>
{
new Array(10).fill(0).map((item, index) =>
<li
key={index}
style={
{
height: 20,
background: '#ccc',
margin: 10
}}
ref={el => { domRefs.current[index] = el }}
onClick={() => { console.log(domRefs, domRefs.current[index]) }}
>
{index + 1}
</li>
)
}
</ul>
)
}
边栏推荐
- Draw a circle and a square on the screen. The square is in front and the circle is behind. You can move the square through the keyboard. In the following cases, the square can only move within the cir
- 函数闭包
- 详解浏览器和Node的事件循环机制及区别
- Mobile software development ISO simple wechat
- php的多选、单选结果怎么在前台显示?
- Modify jupyter save path
- C document reading and writing plus linked list addition, deletion, modification and query
- vulnhub-SolidState: 1靶机渗透测试
- 开启Web3,曾经冷门的去中心化身份(DID)
- select_ Render small phenomena
猜你喜欢
随机推荐
LaTeX学习笔记(一)——安装配置
OPENGL在屏幕上绘制2个点,右边一个蓝色的点,采用反走样技术,左边一个红色的点,不采用反走样技术。比较两个点的区别。
收藏==学废
新建 umi 项目,Error: Rendered more hooks 或者 Rendered fewer hooks
Summary of data types
3. Draw a five sided cone with a square bottom on the screen. The bottom of the cone is on the xoz plane and the top of the cone is on the Y axis. Use the following figure to map the texture of the fo
Modify jupyter save path
在屏幕上绘制一个运动的茶壶,茶壶先慢慢向屏幕里面移动,越变越小,越变越模糊;然后慢慢变大,越变越清晰,一直往返重复。为场景添加光照,材质和雾效果。通过键盘’a’’s’’d’来选择不同的雾效模式
详解浏览器和Node的事件循环机制及区别
special effects - 蜘蛛网背景特效
C document reading and writing plus linked list addition, deletion, modification and query
Canvas - rotate
haclabs: no_name(HL.ova)靶机渗透-Vulnhub
New grammar 01_ ES6 new syntax
随意写写 cookie,sessionStorage,localStorage和session
按钮 渐变
GeoServer automatically uploads shapefiles
Tabs标签页(el-tabs)_造成页面卡死问题
Hurry in!! Easily master the three structures of "sequence", "branch" and "cycle" of C language
Insanity:1(Insanity-Hosting)靶机渗透 —Vulnhub









