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

228
Views
¿Cómo calcular el ancho del componente en reaccionar js?

Estoy tratando de calcular el ancho del componente secundario. ¿Es posible calcular el ancho de los componentes secundarios? aquí está mi código aquí está mi código https://codesandbox.io/s/reverent-hermann-yt7es?file=/src/App.js

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

TABLAS.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;

Sé que al usar ref podemos acceder a la propiedad dom, pero cómo aplicar ref en el elemento de elementos secundarios. aquí estoy tratando de calcular el ancho de todos los artículos

 children.forEach((tab, index) => { if (tab) { console.log(tab);
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Simplemente responda a su pregunta sobre cómo obtener la referencia del niño.

 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> }

NOTA: la forma en que paso ref aquí es exactamente la misma forma en que pasa el método principal al secundario. Y también ref admite dos formatos, estoy usando el simple, pero también puede usar el formato () => {} . https://reactjs.org/docs/refs-and-the-dom.html

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!