knowledge
Hook
Began in16.8 the React
Hook
Not in theclass
The use of- Can only be called from the outermost layer of the React component function
Hook
(customHook
Exception), cannot be called in loops, conditional judgments, or subfunctions Hook
The update function returned is similarthis.setState
But will not be old and newstate
Merge, and herestate
It doesn’t have to be an object- If the name of the function is
use
Start and call otherHook
This is a customizationHook
React
Guaranteed every runeffect
At the same time,DOM
All have been updatedeffect
The cleanup phase is performed on each re-render,React
Will be executed in the currenteffect
I had one beforeeffect
To remove- Customization between different components
Hook
Don’t sharestate
The learning
- Why do you need to go in the first place
Hook
Because this one is a little hard to understand, let’s go and have a lookHook
What can you do, think while you’re watchingHook
The advantages of
legacy
Hook
Technology makes it impossible to writeclass
In case of usestate
Becomes possible (code below). But to be honest, the first time I saw this code, I had no idea how it worked.setCount
How does it trigger the component redraw without tellingimport React, { useState } from 'react'; function Example() { // Declare a new state variable called "count" const [count, setCount] = useState(0); return ( <div> <p>You clicked {count} times</p> <button onClick={()= > setCount(count + 1)}> Click me </button> </div> ); } Copy the code
React
The official documentation says it depends on the order of Hook callsstate
和useState
Why do we need to know the corresponding relationship between the two? This puzzled me.useCallback
,useMemo
,useRef
The three apis lack examples and are unreadable when read