W
W
Wadnory2022-02-05 16:27:30
React
Wadnory, 2022-02-05 16:27:30

How to fix error in React: "Too many re-renders"?

<>
         <h2 className='about-me__text'>
          ...
         </h2>
         <h4 className='about-me__text'>
           ...
         </h4>
         <p className='about-me__text'>
           ...
         </p>
         <p className='about-me__text'>
           ...
         </p>
         <p className='about-me__text'>
           ...
         </p>
</>


---

const [textNodes, setTextNodes] = React.useState()
setTextNodes(Array.from(document.querySelectorAll('.about-me__text')))


Gives an error message:

Uncaught Error: Too many re-renders. React limits the number of renders to prevent an infinite loop.


What's wrong?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
@
@insighter, 2022-02-05
@Wadnory

const [textNodes, setTextNodes] = React.useState(()=>Array.from(document.querySelectorAll('.about-me__text')))

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question