Hook
useState
useState() returns an array with exactly two elements:
(1) Your current state value, (2) A method to update your state value.
useState() will replace the old state instead of merging.
import React, { useState } from "react";
import Person from "./Person/Person";
const App = props => {
const [personState, setPersonState] = useState({
person: [
{ name: "Harry", age: 24 }
]
});
const [otherState, setOtherState] = useState("some other value");
const switchNameHandler = () => {
setPersonState({
person: [
{name: "Harry Lu", age: 25}
]
});·
};
return (
<div className="App">
<button onClick={switchNameHandler}>Switch Name</button>
<Person
name={personState.person[0].name}
age={personState.person[0].age}
/>
</div>
);
}
export default App;useEffect & useRef & useContext
useEffect: componentDidMount · componentDidUpdate componentWillUnmount
UseContext
recieved a React.createContext
return ---- <MyContext.Provider> value prop
Counter Example
React Counter using Hooks
Using the Effect Hook
useEffect lets us express different kinds of side effects after a component renders. Some effects might require cleanup so they return a function
Use Multiple Effects to Separate Concerns
you can use the State Hook more than once, you can also use several effects. This lets us separate unrelated logic into different effects:
Custom Hook
A custom Hook is a JavaScript function whose name starts with ”use” and that may call other Hooks.
Class component to functional
Hook Use
Only in top of react function. Not in conditional control language
react use linked list hooks to rank

Update:

Last updated
Was this helpful?