Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

array of refs react

import React, { useRef, useEffect } from 'react';
import ReactDOM from 'react-dom';

const words = ['Many', 'Words'];

const App = () => {
  const wordsRef = useRef([]);

  const displayWords = words.map((word, i) => (
    <React.Fragment key={i}>
      <span ref={el => (wordsRef.current = [...wordsRef.current, el])}>
        {word}
      </span>
    </React.Fragment>
  ));

  useEffect(() => {
    console.log(wordsRef);
    console.log(wordsRef.current[0].classList);
  });

  return <div>{displayWords}</div>;
};

ReactDOM.render(<App />, document.getElementById('root'));
Comment

PREVIOUS NEXT
Code Example
Javascript :: convert base64 to image nodejs 
Javascript :: javascript replace text within dom 
Javascript :: js inline if 
Javascript :: check date format javascript 
Javascript :: javascript check if number is multiple of 3 
Javascript :: what is data node in big data 
Javascript :: react onclick function 
Javascript :: get cursor position in contenteditable div 
Javascript :: jquery ready 
Javascript :: react detect enter key 
Javascript :: Error: Requires Babel "^7.0.0-0", but was loaded with "6.26.3". 
Javascript :: remove attribute onclick jquery 
Javascript :: save text to file nodejs 
Javascript :: socket.io reconnect example 
Javascript :: javascript store text file into string 
Javascript :: jquery insertafter 
Javascript :: javascript range of integers with spread 
Javascript :: tabuada js 
Javascript :: react img not showing 
Javascript :: commonjs vs es6 
Javascript :: como calcular porcentaje en javascript 
Javascript :: svelte ondestroy 
Javascript :: moment js react 
Javascript :: javascript extract hour from string 
Javascript :: js bundle with popper bootstrap 
Javascript :: data table 
Javascript :: javascripte list length 
Javascript :: map function in react 
Javascript :: prime number js 
Javascript :: all inputs under div 
ADD CONTENT
Topic
Content
Source link
Name
4+4 =