Using React Hooks Flashcards
What does useEffect
do in React?
Executes side effects in functional components.
What are the two parameters of the useEffect
hook?
Effect function and an optional dependency array.
What happens when useEffect
is used without dependencies?
The effect runs on every render.
When does useEffect
run when dependencies are provided?
It runs when the dependencies change.
How do you clean up side effects in useEffect?
Return a cleanup function from the effect.
What is the purpose of the useEffect
hook in data fetching?
To trigger side effects like fetching data after the initial render.
What are the rules for using React hooks?
Call hooks at the top level, not conditionally, and only in function components.
What is the purpose of useReducer
?
It is an alternative to useState
for managing more complex state logic.
What does useReducer
return?
A tuple: current state and dispatch function.
How does a reducer function work in React?
It describes state changes based on actions.
What should you avoid when updating state with useReducer
?
Mutating the state directly. Always return a new state object.
How do you reference HTML elements imperatively in React?
Use useRef
to create a mutable object that persists across renders.
What does useMemo
do in React?
Memoizes a computed value to optimize performance by recalculating only when dependencies change.
How does useCallback
optimize performance?
It memoizes functions to prevent re-creation on each render.
How can you prevent unnecessary re-renders of child components?
Use useCallback
or React.memo to memoize functions and components.
What triggers a component re-render in React?
A state change triggers a re-render of the component and its children.
What does React.memo do?
Memoizes a component to prevent unnecessary re-renders, improving performance.
How does memoization help with performance optimization?
It avoids unnecessary re-renders, particularly in computationally expensive components.