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

118
Views
Children do not update on props change via Ref

I have the following React component:

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

function Child({ containerWidth }) {
  console.log("CHILD", containerWidth);
  return <div>My parent's width is {containerWidth}px</div>;
}

export default function App() {
  const containerRef = useRef(null);
  const [containerWidth, setContainerWidth] = useState(null);
  const [width, setWidth] = useState(400);

  console.log("PARENT", containerWidth);

  useEffect(() => {
    if (containerRef.current) {
      setContainerWidth(containerRef.current.clientWidth);
    }
  }, [containerRef]);

  const handleWidth = () => setWidth(1000);

  console.log("width", width);

  return (
    <div ref={containerRef} style={{ width: width }}>
      <Child containerWidth={containerWidth} />
      <button onClick={handleWidth}>change my width</button>
    </div>
  );
}

I need the children to be always aware and re render whenever the width changes. The value of this ref stays the same even when we click on the button to change the width.

I've tried calling a function with useCallback but no success as the ref never changes.

 const getChild = useCallback(
    () => <Child containerWidth={containerWidth} />,
    [containerWidth]
  );

(.....)

  return (
    <div ref={containerRef} style={{ width: width }}>
      {getChild()}
      <button onClick={handleWidth}>change my width</button>
    </div>
  );
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

It is working actually but once because you are checking if ref exist or no. Once it exist then it will not trigger the useEffect logic anymore. You can manage it with event listener inside useEffect. I just created an example demo for you. Here is the working codesandbox https://codesandbox.io/embed/wild-flower-ytv2b3?fontsize=14&hidenavigation=1&theme=dark

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

function Child({ containerWidth }) {
  console.log("CHILD", containerWidth);
  return <div>My parent's width is {containerWidth}px</div>;
}

export default function App() {
  const containerRef = useRef(null);
  const [containerWidth, setContainerWidth] = useState(window.innerWidth);
  const [width, setWidth] = useState(400);

  console.log("PARENT", containerWidth);

  const handleResize = () => {
    setContainerWidth(window.innerWidth);
  };

  useEffect(() => {
    window.addEventListener("resize", handleResize, false);
  }, []);

  const handleWidth = () => setWidth(containerWidth);

  console.log("width", width);

  return (
    <div ref={containerRef} style={{ width: width }}>
      <Child containerWidth={width} />
      <button onClick={handleWidth}>change my 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!