React-Effects Flashcards
When is a component “mounted” to the DOM?
After the component has been rendered or called.
What is a React Effect?
Effects let you specify side effects that are caused by rendering itself, rather than by a particular event.
When should you use an Effect and when should you not use an Effect?
You should use an Effect only when you are stepping out of your React code and want to sychronize with an external system
If there is no external system involved (for example, if you want to update a component’s state when some props or state change), you shouldn’t need an Effect
When do Effects run?
after the component has rendered, unmounted, and when the prop value has changed and appeared on the screen
What function is used to declare an Effect?
useEffect()
What are Effect dependencies and how do you declare them?
Effect dependencies stops the Effect from running after every render of the component if the dependency is the same as it was during the previous render
Why would you want to clean up from an Effect?
If the user is switching from one component to another. There are instances where you would want to stop and undo the effect from running again
How do you clean up from an Effect?
usinng the return keyword followed by the cleanup function
When does the cleanup function run?
React will call your cleanup function each time before the Effect runs again