Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

188
Views
How to traverse in nested elements of React ref

Hello I'm trying to get all a tags inside #header-nav

const HeaderNav = (props) => {
// setState
const $target = useRef(null);
const $component = $target.current.querySelectorAll('a');

return (
    <nav id='header-nav' ref={$target}>
        <div id='shortcuts'>
            <a className='resources' href='#'>
                <span>0</span>
                <WidgetsIcon />
            </a>
            <a className='trash' href='#'>
                <DeleteSweepIcon />
            </a>
            <a className='history' href='#'>
                <RestoreIcon />
            </a>
        </div>
    </nav>)}

but running querySelectorAll on Ref.current doesn't work

    const $target = useRef(null);
    const $component = $target.current.querySelectorAll('a');

It returns null, probably because querySelectorAll refers to live nodes which haven't been rendered yet Also since I have many a tags (like 20), putting multiple different refs and creating an array of refs doesn't seem like a best solution

How can I traverse inside ref result?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

We should put useRef result inside useEffect hook

const $target = useRef();
    useEffect(() => {
        const $component = $target.current.querySelectorAll('a');
    }, []);
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!