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

225
Vistas
how to calculate width of component in react js?

I am trying to calculate the width of child component .is it possible to calculate width of children's ? here is my code here is my code https://codesandbox.io/s/reverent-hermann-yt7es?file=/src/App.js

<Tabs>
      {data.map((i) => (
        <li>{i}</li>
      ))}
    </Tabs>

TABS.js

import React, { useEffect, useRef } from "react";

const Tabs = ({ children }) => {
  const tabsRef = useRef(null);

  const setTabsDimensions = () => {
    if (!tabsRef.current) {
      return;
    }

    // initial wrapper width calculation
    const blockWidth = tabsRef.current.offsetWidth;
    // const showMoreWidth = moreItemRef.current.offsetWidth;

    // calculate width and offset for each tab
    let tabsTotalWidth = 0;
    const tabDimensions = {};

    children.forEach((tab, index) => {
      if (tab) {
        console.log(tab);
        
        // const width = !isMobile ? 200 : 110;

        // tabDimensions[index] = {
        //   width,
        //   offset: tabsTotalWidth
        // };
        // tabsTotalWidth += width;
      }
    });
  };

  useEffect(() => {
    setTabsDimensions();
  });
  return (
    <ul ref={tabsRef} className="rc64nav">
      {children}
    </ul>
  );
};

export default Tabs;

I know using ref we can access the dom property but how to apply ref in children element element. here i am trying to calculate all item width

   children.forEach((tab, index) => {
              if (tab) {
                console.log(tab);
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

Just answer your question how to get child's ref.

  const Parent = () => {
    const childRef = useRef()

    // An example of use the childRef
    const onClick = () => {
     if (!childRef.current) return
      
     console.log(childRef.current.offsetWidth)
    }


    return <Child childRef={childRef} />
  }

  const Child = ({ childRef }) => {
   return <div ref={childRef}>Hello Child</div>
  }

NOTE: the way I pass ref here is exactly the same way you pass parent method to child. And also ref supports two formats, i'm using the simple one, but you can also use () => {} format. https://reactjs.org/docs/refs-and-the-dom.html

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