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

238
Vistas
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
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