How target DOM with react useRef in map

后端 未结 3 1910
一生所求
一生所求 2020-12-04 23:48

I looking for a solution about get an array of DOM elements with react useRef() hook.

example:

const Component = () => 
{

  // In `         


        
3条回答
  •  一个人的身影
    2020-12-05 00:40

    If you know the length of the array ahead of time, to which you do in your example you can simply create an array of refs and then assign each one by their index:

    const Component = () => {
      const items = Array.from({length: 2}, a => useRef(null));
      return (
        
      {['left', 'right'].map((el, i)) => (
    • {el}
    • )}
    ) }

提交回复
热议问题