Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

94
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!