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

121
Vistas
React too many re-renders because of data loading?

I have this chunk of code:

export default function CloseInquiryComponent(props) {

    const auth = getAuth();
    const db = getFirestore();

    const { id } = useParams();

    const [someVar, setSomeVar] = useState("");

    // get user 
    const [user, authLoading, authErr] = useAuthState(auth);
    // get user data of a collection
    const [userData, userDataLoading, dataErr] = useDocumentData(
      user ? doc(db, `users/${user.uid}`) : null,
    );

    // get the inquiry data
    const [inquiry, inquiryLoading, inquiryError] = useDocumentData(doc(db, "תקלות", id));

    // show loading until data is done loading
    if(inquiryLoading)
    {
        return <LoadingComponent/>
    }

    // this causes an error because there is a return before it.
    setSomeVar(inquiry.SomeData);

If I remove the if statement and try to set there, it might be null. If I try something like:

if(!inquiryLoading)
{
    setSomeVar(inquiry.SomeData);
    return <pagehtmlhere/>
}

I get an error "too many re-renders".

each re-render calls the function and sets the variable, which causes more re-renders.

How can I set the data once it's been loaded, while also keeping the react rules in mind?

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

Use useEffect hook.

useEffect(() => {
    if (inquiryLoading && inquiry) setSomeVar(inquiry.SomeData);
}, [inquiryLoading, inquiry])
about 4 years ago · Juan Pablo Isaza Denunciar
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