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

381
Visualizações
Error: Maximum update depth exceeded. setState throws error
export class ABC extends React.Component {

  constructor(props) {
    super(props);
    this.state = {
      abc: null
    };
  }

  renderOptions() {
      this.setState({
        abc: abcArray.length !== 0
      });
    return;
  }

  renderRadio() {
    return (
          <Field
            id="abc"
            name="abc"
            values={this.renderOptions()}
          />
    );
  }

  render() {
    return (
      <div>
               {this.renderRadio()}

      </div>
    );
  }
}


export default connect(mapStateToProps)(ABC);

I am trying to setState in renderOptions() which gives me the below error.

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.

can't get my head around what im doing wrong here.

any help is appreciated.

about 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

Your render method is calling setState, which will trigger render to be called again.

You should not call setState inside of renderOptions.

about 4 years ago · Santiago Trujillo Relatório

0

in renderRadio() -> values={this.renderOptions()} you are calling the renderOptions function. that function calls the this.setState({abc: abcArray.length !== 0});. then react will try to rerender the component. this will create a loop. to avoid it, you should change values prop to this values={this.renderOptions}

I've refactor your code.

export class ABC extends React.Component {

  constructor(props) {
    super(props);
    this.state = {
      abc: null
    };
  }

  renderOptions() {
      this.setState({
        abc: abcArray.length !== 0
      });
    return;
  }

  renderRadio() {
    return (
          <Field
            id="abc"
            name="abc"
            values={this.renderOptions}
          />
    );
  }

  render() {
    return (
      <div>
               {this.renderRadio()}

      </div>
    );
  }
}


export default connect(mapStateToProps)(ABC);

about 4 years ago · Santiago Trujillo 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