current position:Home>Simple introduction to react -- the use of usestate in common hooks

Simple introduction to react -- the use of usestate in common hooks

2022-01-27 01:06:48 Poker_ Xu

Preface :
stay react in , We prefer to use... At the beginning of our study class Components , But in real development , More of it Function component , But in function components , It's hard to be like class Components Have state Characteristics of , To manage some states of the current component , So in the function component, we can refer to useState Of hook

1、 Import :

This method does not require downloading third-party packages , Directly in react Just refer to it in the package ,
 Insert picture description here

2、 Use :

Let's create a home Function components
 Insert picture description here
Show the effect :
 Please add a picture description
We found that we can achieve something similar class Components You can also manage your own components state 了 , Its syntax is roughly as follows :

let [ state, setState ] = useState( params )

Let's take a look at this useState() function What's printed out
 Insert picture description here
Print found , We give useState() What to pass on params Parameters , It will return a corresponding containing With a params Equal value and A function Array of , And the first thing in the array is actually our state, The second thing is to manage this state Function of , also , We can just use array deconstruction to accept two things of this array , The grammar I appealed against appeared . If we pass the test again, we can really achieve our goal , Of course , We often have more than one component state, In fact, when I print, you should also be able to observe , You want more state, Then call a few more useState() Function .

copyright notice
author[Poker_ Xu],Please bring the original link to reprint, thank you.

Random recommended