React Hook – Prev State

React Hook – Prev State

In state component we can use the following idiom

state = {
   count: 0
}
addCount = () => {
   this.setState( prevState => ({
      count: prevState.count + 1
   }));
} 

In react hooks, we do similar like below.

const [count, setCount] = useState(0);
const addCount = () => {
   setCount(prevCount => prevCount + 1);
}

Write a Reply or Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.