@@ -5,24 +5,24 @@ you use special functions called "hooks" to do this. Common built-in hooks
5
5
include:
6
6
7
7
- ` useState `
8
- - ` useEffect `
9
- - ` useContext `
10
8
- ` useRef `
9
+ - ` use ` (currently in canary)
11
10
- ` useReducer `
11
+ - ` useEffect `
12
12
13
13
Each of these is a special function that you can call inside your custom React
14
14
component function to store data (like state) or perform actions (or
15
15
side-effects). There are a few more built-in hooks that have special use cases,
16
16
but the ones above are what you'll be using most of the time.
17
17
18
18
Each of the hooks has a unique API. Some return a value (like ` useRef ` and
19
- ` useContext ` ), others return a pair of values (like ` useState ` and
19
+ ` use ` ), others return a pair of values (like ` useState ` and
20
20
` useReducer ` ), and others return nothing at all (like ` useEffect ` ).
21
21
22
22
Here's an example of a component that uses the ` useState ` hook and an onClick
23
23
event handler to update that state:
24
24
25
- ``` jsx
25
+ ``` tsx
26
26
function Counter() {
27
27
const [count, setCount] = useState (0 )
28
28
const increment = () => setCount (count + 1 )
@@ -51,6 +51,11 @@ called this time, the value we get back is the value that we called `setCount`
51
51
with. And it continues like that until ` Counter ` is unmounted (removed from the
52
52
application), or the user closes the application.
53
53
54
+ Note that after the initial render, the argument passed to ` useState ` is
55
+ ignored. The only time it's used is when the component is first created. The
56
+ only way to change the state value of the component while it's around is to
57
+ call the updater function returned by ` useState ` .
58
+
54
59
π To get a deeper dive on how React keeps track of hooks, read/watch this great
55
60
post/talk by [ Shawn Wang] ( https://twitter.com/swyx ) :
56
61
[ Getting Closure on Hooks] ( https://www.swyx.io/getting-closure-on-hooks/ )
0 commit comments