Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

140
Visualizações
how to pass a function through multiple nested loops in react

I'm trying to pass a function called 'clicker()' through multiple nested loops. I get the following error:

Maximum update depth exceeded. This can happen when a component repeatedly calls setState inside componentWillUpdate or componentDidUpdate. React limits the number of nested updates to prevent infinite loops.

The clicker() function follows this pathway:

app.js --> one.js --> two.js --> three.js

How can I pass this function, initiated in app.js all the way through until it gets to the last component. Note that app.js renders one and this renders two and this renders three.

here is my codesandbox.

app.js:

import React, { useState } from "react";
import One from "./components/one";

export default function App() {
  const [counter, updateCounter] = useState(0);
  let clicker = () => {
    updateCounter(counter + 1);
  };

  return (
    <div className="App">
      <h1>how to pass a function through nested components in React </h1>
      <h3> {counter} </h3>
      <One passMe={clicker} />
    </div>
  );
}

and one.js

import Two from "./two";

function One(props) {
  return (
    <div className="App">
      <p> this is the nested function</p>
      <Two passMe={props.passMe} />
    </div>
  );
}
export default One;

and two.js

import React from "react";
import Three from './three'

export default function Two(props) {


  return (
    <div className="App">
<Three passMe={props.passMe} />
    </div>
  );
}

and finally three.js

import React from "react";

export default function App(props) {


  return (
    <div >
      <button passMe={props.passMe(5)}>Cliicker! </button>
    </div>
  );
}

thanks so much

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

Why are you passing 5 as parameter, if the function does not support any?

Assuming you want that, you may change this:

On App.js:

const clicker = (num = 1) => {
    updateCounter(counter + num);
};

On three.js:

<button onClick={() => props.passMe(5)}>Cliicker!</button>
about 4 years ago · Juan Pablo Isaza Relatório

0

Actually you are on the right track of passing a function from a parent to its nested child components. But assuming you want to update the counter whenever user clicks the button on its nested component, you might want to pass the onClick(()=> props.passMe(anyValue)) on three.js button component. This way, the function created in the parent gets invoked on every button click all the way from three.js, two.js, one.js to app.js.

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda