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

165
Visualizações
Prevent infinite call to setState in React Class component

I have a React class component with the following code (only included the relevant stuff):

class SomePage extends Component {

  constructor(props) {
    super(props);
     this.state = {
      stuff: ""
    }
  }

  // NOTE: this.props.data comes from mapStateToProps from below

  componentDidMount() {
     console.log(this.props.data)   // shows up as "undefined"
  }

  componentDidUpdate(prevProps) {
    console.log(this.props.data);   // has the data I need

    if((prevProps.data !== this.props.data) && this.props.data.part1 && this.props.data.part2) {  // LINE A
      console.log("inside if statement");     // never entered
      this.setState({stuff: this.props.data.part2});      // need to set state here!!
    }
  }

  render () {
    return (
      {this.state.stuff}
    )
  }

  const mapStateToProps = state => {
    return {
       data: state.analysisReducer.data;
    }
  }

export default connect(mapStateToProps, null)(SomePage);

If I change the componentDidUpdate from above to not include prevProps in the if statement, as shown below, then the setState is called infinitely.

componentDidUpdate(prevProps) {
  console.log(this.props.data);   // has the data I need

  if(this.props.data && this.props.data.part1 && this.props.data.part2) {                 // LINE B - removed the "prevProps.data !=="
    this.setState({stuff: this.props.data.part2});  // entered, but called infinitely
  }
}

Problem: As shown above, Line A is never entered and the setState in Line B causes an infinite loop.

Goal: I need to call the setState only once, either when I get access this.props.data once its data becomes available or when the this.props.data changes from the its previous state.

How do I do this?

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

0

You are not modifing the data props that you are passing so you don't need to put that in components state. You can skip all the state stuff and just use the props that you are passing.

// Added after comment You are still not editing the data passed from props, but here is an example of how to do what you want with a functional component.

import { useEffect, useState } from "react";

export default function App() {
  const [input, setInput] = useState('');

  return (
    <div className="App">
      <h1>Hello</h1>
      <input type='text' value={input} onChange={(e) => setInput(e.target.value)} />
      <Page data={{ part2: input }} />
    </div>
  );
}

const Page = (props) => {
  const [state, setState] = useState({});

  useEffect(() => {
    console.log("useEffect");
    setState({
      ...state,
      stuff: props.data.part2
    });
  }, [props.data.part2]);

  return <div>{state.stuff}</div>;
};

Codesandbox: https://codesandbox.io/s/adoring-varahamihira-bgqpby

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