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

191
Views
Passing HTML element into React Component before Element is Rendered

If we have the React component Foo that instantiates the class Bar and we need to pass the HTMLCollection element with ID foo into Bar, how can it be done?

Bar.js should ideally remained unchanged.

I tried the following:

Foo.js

import Bar from './Bar';

const Foo = () => {
    const elem = document.getElementById('foo');
    const bar = new Bar(elem, {});

  return (
    <div id="foo">
    </div>
  );
};

export default Foo;

Bar.js

export default class Bar {
    constructor(domElement, config = {}) {
      console.log(domElement);  // null
      console.log(domElement.getElementsByClassName('bar'));  // null
    }
  }

but domElement is always null, maybe because when we run document.getElementById, the element div#foo has not been rendered yet.


Also tried the using useRef:

Foo.js

import { useRef } from 'react';
import Bar from './Bar';

const Foo = () => {
  const elemRef = useRef(null);
  const bar = new Bar(elemRef, {});

  return (
    <div id="foo" ref={elemRef}>
    </div>
  );
};

export default Foo;

Bar.js

export default class Bar {
    constructor(domElement, config = {}) {
      console.log(domElement);  // {current: null}
      console.log(domElement.getElementsByClassName('bar'));  // Uncaught TypeError: domElement.getElementsByClassName is not a function
    }
}

but getting the error

Uncaught TypeError: domElement.getElementsByClassName is not a function

What is the correct way to do this?

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

0

You can use useLayoutEffect hook which fires synchronously after all DOM mutations

useLayoutEffect(() => {
    const elem = document.getElementById("foo");
    const bar = new Bar(elem, {});
  }, []);
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!