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);
}
React Hook – Prev State

Leave a Reply

Your email address will not be published. Required fields are marked *

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