Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

96
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda