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

239
Views
Why is document.getElementsByClassName not working in react?

So I have this piece of Code

const desc_object = document.getElementsByClassName("singlePostDesc");
console.log(desc_object[0]);

And this is the JSX

{updateMode ? (
                        <>
                            <textarea
                                className="singlePostDescInput"
                                autoFocus={true}
                                id="desc"
                                onChange={(e) => setDesc(e.target.value)}
                                onKeyPress={(e) => key_callback(e.key)}
                            >
                                {desc}
                            </textarea>
                            <button
                                className="singlePostButton"
                                onClick={update_post_to_backend}
                            >
                                Update Post
                            </button>
                        </>
                    ) : (
                        <div className="singlePostDesc" id="descThing">
                            <ReactMarkdown children={sanitizeHtml(desc)} />
                        </div>
                    )}

But I get the result as undefined. Why? Is it because it is wrapped in a ternary operator? I am using React JS.

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

0

In order to access DOM methods in react, you have to call this piece of code inside useEffect hook.

useEffect(() => {
  const desc_object = document.getElementsByClassName("singlePostDesc");
  if (desc_object) {
   // now you can access it here
  }

})

though the first time useEffect hooks runs getElementsByClassName will return undefined because the DOM is not mounted yet.

if you want to run this DOM query only after the component did mount, you can create a custom hook:

const useDidMountEffect = (func, deps) => {
 const didMount = useRef(false)

 useEffect(() => {
   if (didMount.current) func()
   else didMount.current = true
  }, deps)
}

and then you can use it like that:

useDidMountEffect (() => {
 const desc_object = document.getElementsByClassName("singlePostDesc");


})

UPDATE:

as comments mentioned, ideally you should not use vanilla js, you should use React Refs

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!