Senior Side Week 4 Flashcards
When is a component “mounted” to the DOM?
When the component appears on the page for the first time.
What is a React Effect?
In React, an effect is a function that is executed automatically by React after rendering and updating a component. Effects are typically used for performing side-effects in response to changes in a component’s state or props.
When should you use an Effect and when should you not use an Effect?
Effects should be used in React components when you need to execute some side-effect, such as updating the DOM, fetching data from an API, setting up event listeners, or updating the component’s state or props in response to changes.
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?
In React, effects run after every render of a component by default. This includes the initial render and subsequent re-renders due to changes in the component’s state or props.
What function is used to declare an Effect?
useEffect();
What are Effect dependencies and how do you declare them?
In React, effect dependencies are used to tell React when an effect should run. Effect dependencies are specified as an array of values that are passed as the second argument to the useEffect hook.
When an effect is declared, React will compare the previous dependencies array to the current dependencies array. If any of the values in the current dependencies array are different from the previous dependencies array, React will run the effect function again.
Why would you want to clean up from an Effect?
Some Effects need to specify how to stop, undo, or clean up whatever they were doing.
Removing event listeners: If you set up event listeners in an effect, you should also remove them in a cleanup function to prevent memory leaks and unexpected behavior.
Cancelling network requests: If you fetch data from an API in an effect, you should also cancel the request in a cleanup function to prevent memory leaks and to ensure that the data is not updated after the component unmounts.
Cleaning up timers and intervals: If you use timers or intervals in an effect, you should also clear them in a cleanup function to prevent memory leaks and unexpected behavior.
How do you clean from an Effect?
To clean up from an effect, you can return a function from the effect function that will be executed when the component unmounts or when the effect is re-run. This function is called the cleanup function.
When does the cleanup function run?
React will call your cleanup function each time before the Effects run again, and one final time when the component unmounts (get removed).
What is the purpose of the Express Static middleware?
To serve static files such as images, CSS files, Javascript files, use the express static built-in middleware function in Express. Express stattic sets up middleware that checks to see if a file exists and then calls the sendFile on it.
What does express.static() return?
It returns a middleware function.
What are several examples of static files?
HTML files, CSS files, JavaScript files, images
What is a good way to serve application images using Express?
Use the express static-built in middleware function.
What does ‘fetch()’ return?
It returns a promise that resolves with a response (a representation of the entire HTTP response)object
What is the default request method used by ‘fetch()’?
Get