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

235
Views
Most efficient way to send child refs back to parent in React?

Given the below example:

import React, { useState, useEffect, useRef } from "react";
import id from "uniqid";

const Child = ({ updateRef, editorKey }) => {
  const ref = useRef();

  useEffect(() => {
    if (ref.current && editorKey) {
      updateRef(editorKey, ref.current);
    }
  }, [ref.current]);

  return <span ref={ref}>child</span>;
};

const Parent = () => {
  const [resolutions, setResolutions] = useState([{ key: id(), ref: null }]);

  const updateRef = (key, ref) => {
    const resolutionsCopy = [...resolutions];
    const index = resolutionsCopy.findIndex((value) => value.key === key);
    resolutionsCopy[index].ref = ref;

    setResolutions(resolutionsCopy);
  };

  console.log(resolutions);

  return resolutions.map((res, idx) => {
    return <Child key={res.key} editorKey={res.key} updateRef={updateRef} />;
  });
};

export default Parent;

Is there a better way to effectively send back children refs back to parent (1 source of truth) while minimizing the amount of re-renders? The resolution state might have more variables that handle how children render, but the update in ref shouldn't re-render the children, but only update the states null ref to the actual ref.

Been trying to memoize it, but it always results in infinite re-renders. Advice would be helpful.

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!