React useEffect Flashcards
Give three examples of effects.
What are effects, really? Examples are:
Fetching data
Reading from local storage
Registering and deregistering event listeners
How should you write asynchronous code in react?
Using useEffect( )
Is it a good practice to use more than one useEffect in a component ?
It is, don’t be afraid to use multiple useEffect statements in your component. While useEffect is designed to handle only one concern, you’ll sometimes need more than one effect.
Is useEffect designed to handle only one concern?
Yes useEffect is designed to handle only one concern. If you have more than one concern you need to add more useEffects.
What are the problems of using only one effect for multiple purposes?
It decreases the readability of your code, and some use cases are straight-up not realisable.
Should you think about hooks in terms of lifecycle methods?
No, don’t think in lifecycle methods anymore!
When are effects executed?
After render. To be more specific, they execute both after the first render and after every update.
Can effects block the UI?
No effects don’t block the UI because they run asynchronously.
What determines whether an effect will be executed?
Other than always being ran after first render, the dependency array determines whether an effect will be executed. If a dependency array does not exist then an effect will always be executed. If a dependency array does exist it will only run an effect if a change has been made to the dependency array.
What are acceptable Array values for useEffect dependency arrays and where must they come from?
Dependency array values must be from the component scope (i.e., props, state, context, or values derived from the aforementioned).
Changes to what three things can cause a component to be re-rendered.
Changes to state, prop, or context cause components to be re-rendered.
What are the “conditions” to execute for a useEffect call?
In the case of useEffect the “conditions” to execute are whether a useEffect dependency array exists or not and if it does have any values changed since the last render cycle.
If no dependency array is present an effect is executed after every re-render.
If a dependency array exists an effect is only executed if the dependency array’s values have changed.
Does a useEffect always get called on a components initial render?
Yes it does, it runs after first render and then after every update (assuming if a dependency array exists, it has changed).
What do the hooks useState, useEffect and useRef give you access too? (this is a stupid question for my own understanding)
useState lets you use state,
useEffect lets you use effects,
useRef lets you use refs
Does the order of your effects matter?
Yes they are executed in the order in which they are declared in the code.