Why do we need setState()?

Need -

setState() is necessary in React to properly update the state of a component and

trigger UI re-renders. It ensures that React is aware of state changes, manages component updates

efficiently, and maintains a consistent UI.

What is it?

setState() is a method in React used to update a component's state. It schedules

a re-render, applies the updated state, and merges it with the existing state. This ensures predictable UI

updates and maintains React's immutability principles for state management.

How is it used?

To use setState() in a React component, first, define the initial state in the

component's constructor or using a class property. Then, call this.setState() within an event handler or

lifecycle method, passing an object with the updated state properties. React will handle state updates and

trigger UI re-renders accordingly.