当前位置:网站首页>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>
)
}
边栏推荐
- 02 mobile terminal page adaptation
- How to forcibly uninstall Google browser? Don't worry about Google opening as a whiteboard. It's effective for personal testing.
- Draw a moving teapot on the screen. The teapot first moves slowly towards the screen, becoming smaller and smaller, becoming more and more blurred; Then it grows bigger and clearer, and keeps repeatin
- Find the flops of the network
- 量化合约夹子套利机器人系统逻辑开发原理分析
- 树状结构+节点
- [[[recursion]]]
- JS输出字符串中出现最多次数的字符
- flex布局
- 字符串_方法_01match方法
猜你喜欢

Node connects to MySQL and uses Navicat for visualization

自定义MVC 2.0

Summary of data types

聊聊强缓存与协商缓存

Mapboxgl + GeoServer configuration local map tutorial

haclabs: no_name(HL.ova)靶机渗透-Vulnhub

动画 效果

jsp标签

How to forcibly uninstall Google browser? Don't worry about Google opening as a whiteboard. It's effective for personal testing.

B站视频评论爬取——以鬼灭之刃为例(并将其存储到csv中)
随机推荐
JS:为什么 [] == ![] 返回 true ?
数据类型概括
[DP] number triangle
模板数据的二次加工
Canvas - rotate
AttributeError: ‘NoneType‘ object has no attribute ‘shape‘
vulnhub-SolidState: 1靶机渗透测试
微信小程序报错request:fail -2:net::ERR_FAILED
C2 random generation function seed, numpy. Random. Seed(), TF. Random. Set_ Seed Learning + reprint and sorting
Canvas - fill
Some thoughts on being a professional
JS链表中的快慢指针
special effects - 鼠标移动,出现泡泡拖尾
MySQL的使用
自定义MVC 3.0
新语法01_Es6新语法
Neo4j修改标签名
网页内嵌B站视频,隐藏相关控件
按钮 渐变
Latex learning notes (I) - installation configuration