What is useState in ReactJs, and why it is used?
All are switching to modern Reactjs i.e Reactjs that works with functional components and when we work with components we need to manage the state, To manage the state we use Hook, A Hook is a special function that lets you “hook into” React features.
What is useState ?
useState
is a Hook that lets you add React state to function components. we can use useState to update the data after a particular event.
useState hook needs to be imported from react as below
How it is used?
It is generally used in a destructured manner where we have a current state here as count and second is a function setCount used when we need to manage change in value.
count -> current value
setCount -> value after change of count
useState(0) means count having initial value zero
Note you can set any initial value
Example
we will add +1 on every count to understand the concept of useState
here we will be setting count =0 and we will make a function that will be invoked when clicking on the button to setCount = count+1
See the below video for output
Code for the same
Follow me on Instagram to learn more about useful concepts of Reactjs