Lesson 10 - Part 2 - Hooks API
Lesson 10 - Part 2 - Hooks API
• Hooks are a new addition in React 16.8. They let you use state
and other React features without writing a class.
• A Hook is a special function that lets you “hook into” React
features..
• Basic Hooks Additional Hooks
• useState useReducer
• useEffect useCallback
• useContext useMemo
useRef
useLayoutEffect
When would I use a Hook?
• If you write a function component and realize you need to add some
state to it, previously you had to convert it to a class. Now you can
use a Hook inside the existing function component. We’re going to do
that right now!
Hooks Rules
• Only call Hooks at the top level. Don’t call Hooks inside loops,
conditions, or nested functions.
• Only call Hooks from React function components. Don’t call Hooks
from regular JavaScript functions.
useState
• Returns a stateful value, and a function to update it.
• During the initial render, the returned state (state) is the same as the
value passed as the first argument (initialState).
• Mutations, subscriptions, timers, logging, and other side effects are not
allowed inside the main body of a function component (referred to as
React’s render phase). Doing so will lead to confusing bugs and
inconsistencies in the UI.
• Instead, use useEffect. The function passed to useEffect will run after the
render is committed to the screen. Think of effects as an escape hatch from
React’s purely functional world into the imperative world.
• By default, effects run after every completed render, but you can choose to
fire them only when certain values have changed.
useEffect - Cleaning up an effect
• Often, effects create resources that need to be cleaned up before the component
leaves the screen, such as a subscription or timer ID. To do this, the function
passed to useEffect may return a clean-up function. For example, to create a
subscription:
• The clean-up function runs before the component is removed from the UI to
prevent memory leaks. Additionally, if a component renders multiple times (as
they typically do)
Did mount and unmount
• If you want to run an effect and clean it up only once (on mount and
unmount), you can pass an empty array ([]) as a second argument.
This tells React that your effect doesn’t depend on any values from
props or state, so it never needs to re-run. This isn’t handled as a
special case — it follows directly from how the dependencies array
always works.
• If you pass an empty array ([]), the props and state inside the effect
will always have their initial values . While passing [] as the second
argument is closer to the
familiar componentDidMount and componentWillUnmount m
ental model.
Conditional update
• You can tell React to skip applying an effect if certain values haven’t
changed between re-renders. To do so, pass an array as an optional
second argument to useEffect:
Week 3 - Day 3
useContext
• The useContext() method is an alternative to prop-drilling through the
component tree and creates an internal global state to pass data.
• Accepts a context object (the value returned from React.createContext)
and returns the current context value for that context. The current context
value is determined by the value prop of the
nearest <MyContext.Provider> above the calling component in the tree.
• Retrieve the value from the one of the child components of <App>
const api = useContext(APIConfig);
useContext
• Don’t forget that the argument to useContext must be the context
object itself:
• Correct: useContext(APIConfig)
• Incorrect: useContext(APIConfig.Consumer)
• Incorrect: useContext(APIConfig.Provider)
useReducer