what is usestate ???
Hooks
To understand usestate, you first need an understanding on what hooks are.
Hooks are the new feature introduced in the React 16.8 version. It allows you to use state and other React features without writing a class or stateless component. Hooks are the functions which "hook into" React state and lifecycle features from function components.
They are like blocks of code(in case of useState, a line of code) that saves us a lot of time when building projects.
UseState
const [state, setState] = useState(0)
this is an instance of the usestate hook, it accepts a parameter, and return two properties, the first property "state" is immutable you can't change it directly, but in the most likely case of you wanting to change the value of that state you, then invoke the second property "setState".
The setState is a method that accepts parameter and reassigns those value to state. and we can dynamically change the value of state without writing a bunch of codes.
p.s. I just learnt what usestates was about and decided to share.
if you have questions feel free to ask.
Top comments (1)
While you can attempt to modify the state value directly, it doesn't produce the intended effect.
beta.reactjs.org/ - new docs provide a great mental model about state and other fundamentals.