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

245
Views
Get child element width in parent component render - React

I have a project that uses basic React components that are compiled to vanilla JS and HTML with Babel. What I want to do is to be able to get the width of the buttons when they render (they can vary depending on the amount of text they have) and use the longest width value in the parent element so I can set both buttons to be that same width I want the CtaContainer to be responsible for setting the width of the button to be the same and aligning them accordingly. I can't set a fixed width as this will only happen when there is more than one button in the container

So first I have this index.js

const Index = () => {
return (
    <div>
        <h1 className={"hl-boldXL"}>Hey</h1>
        <p className={"copy-regularXL"}>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Accusantium animi aspernatur, beatae culpa eius esse in incidunt ipsa, iusto laborum optio placeat porro quam quo, suscipit veniam vero vitae! Dicta?</p>
        <CtaContainer>
            <CtaButton type={"primary"} url={"https://www.someurl.com"} text={"Primary"}/>
            <CtaButton type={"secondary"} url={"https://www.someurl.com"} text={"Secondary"}/>
        </CtaContainer>
    </div>
);};

Then I have a CtaButton component

const CtaButton = (props) => {

return (
    <a className={props.type} href={props.url} title={props.text}>{props.text}</a>
);};

And lastly I have the CtaContainer

const CtaContainer = ({children}) => {
const childRef = useRef(null)

useEffect(() => {
    if(children.length > 1) {
    }
});

return (
    <div>
        {children}
    </div>
);};
about 4 years ago · Juan Pablo Isaza
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!