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

99
Views
clientWidth function in React

I am experienced in working with Javascript, but am pretty new to working with React. When coding purely in javascript that would be rendered in browser, I would often use:

width = document.getElementById('element_id').clientWidth

to dynamically size my svg elements to different container sizes. Does something like this exist in React? I have been trying to use the same technique, but as expected, it does not work because the the template is rendered after the script is run.

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

0

Use can use this it work. useEffect(didUpdate);. Accepts a function that contains imperative, possibly effectful code.

const App = () => {
    useEffect(() => {
        const width = document.getElementById('width').clientWidth;
        console.log({ width });
    }, []);
    
   return(
            <div id="width" />
   );
}
about 4 years ago · Juan Pablo Isaza Report

0

Of course. It is very easy thing to do using "createRef" hook. Basically, document.getElementById search for a DOM element and with createRef you are assigning a reference to (virtual)DOM element and you can do every manipulation on it.

Check how it's done here:

import React, { createRef } from 'react';

export const FooComponent = () => {
  const fooRef = createRef<HTMLDivElement>();

  const handleClick = () => {
    const divClientWidth = fooRef.current?.clientWidth;
    window.alert(divClientWidth);
  };

  return (
    <div ref={fooRef}>
      <button onClick={handleClick}>Show client width</button>
    </div>
  );
};
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!