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

96
Visualizações
Maximum update depth exceeded new state React-Native

const [number,setNum] = useState(0); I get this error when I want to add and change it(setNum(number+1)). My 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. What can i to solve this?

const App = ()=>{
  const [text,setText] = useState('');
  const [todo,setToDo] = useState([]);
  const [number,setNum] = useState(0);
  const renderToDoCard = ({item})=>{
    setNum(number+1)
    return(
    <TouchableHighlight
      onLongPress={() => handleLongPress(item)}>
      <ToDoCard todo={item} number={number}/>
    </TouchableHighlight>
  )
  }
  const handleLongPress = item => {
    setToDo(todo.filter(i => i !== item));
    return Alert.alert('Silindi');
  };
  return(
    <SafeAreaView style={styles.container}>
      <StatusBar backgroundColor='#102027'/>
      <View style={styles.head_container}>
        <Text style={styles.title}>Yapılacaklar</Text>
        <Text style={styles.title}>{todo.length}</Text>
      </View>
      <View style={styles.body_container}>
        <FlatList data={todo} renderItem={renderToDoCard} />
      </View>
      <View style={styles.bottom_container}>
        <ToDoInput todo={todo} setToDo={setToDo} text={text} setText={setText}/>
      </View>
    </SafeAreaView>
  )
}

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

0

You've created an infinite update loop.

The problem is in how you're updating your number state inside renderToDoCard

const renderToDoCard = ({item}) => {
  setNum(number + 1); // This is the problem, remove this line
  return (
    <TouchableHighlight onLongPress={() => handleLongPress(item)}>
      <ToDoCard todo={item} number={number} />
    </TouchableHighlight>
  );
};

When renderToDoCard renders you update the state of your App component so it rerenders App which renders renderToDoCard which updates the state of your App component so it rerenders App which renders renderToDoCard...

This process repeats until the max update depth is reached.

Simply remove setNum(number + 1); and that problem is fixed.

It seems to me from your code that all you use your number state for is to keep track of the current item index so you can pass this to the ToDoCard component. The FlatList's renderItem also provides access to the current item index which you could pass to the number prop of ToDoCard

renderItem({ item, index, separators });

https://reactnative.dev/docs/flatlist#required-renderitem

So you could instead do something like this

const renderToDoCard = ({item, index}) => {
  return (
    <TouchableHighlight onLongPress={() => handleLongPress(item)}>
      <ToDoCard todo={item} number={index} />
    </TouchableHighlight>
  );
};

Alternative you can add a key to each item in todo and use that instead of the index.

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