Senior Side Week 4 Flashcards

1
Q

When is a component “mounted” to the DOM?

A

When the component appears on the page for the first time.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What is a React Effect?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

When should you use an Effect and when should you not use an Effect?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

When do Effects run?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What function is used to declare an Effect?

A

useEffect();

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What are Effect dependencies and how do you declare them?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Why would you want to clean up from an Effect?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

How do you clean from an Effect?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

When does the cleanup function run?

A

React will call your cleanup function each time before the Effects run again, and one final time when the component unmounts (get removed).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What is the purpose of the Express Static middleware?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What does express.static() return?

A

It returns a middleware function.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What are several examples of static files?

A

HTML files, CSS files, JavaScript files, images

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What is a good way to serve application images using Express?

A

Use the express static-built in middleware function.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What does ‘fetch()’ return?

A

It returns a promise that resolves with a response (a representation of the entire HTTP response)object

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What is the default request method used by ‘fetch()’?

A

Get

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

How do you specify the request method(GET, POST, etc.) when calling ‘fetch’?

A

The second parameter of the function.
An object containing any custom settings that you want to apply to the request.

For example:
method: “POST”

17
Q

How does ‘fetch’ report errors?

A

If response ok is false. It becomes false when it status code is not in the 200 range.

18
Q

How can ‘useEffect’ be used to load data for a component?

A

useEffect hook is used to fetch data from an API when the component mounts

19
Q

What browser function can be used to make HTTP requests to a server in React?

A

fetch() function is used to make HTTP requests in react.

20
Q

How do you use useEffect to load component data just once when the component mounts?

A

You can use useEffect to load component data just once when the component mounts by passing an empty dependency array ([]) as the second argument to the useEffect function. This tells React that the effect should only run once, when the component mounts.

21
Q

How do you use useEffect to load component data every time the data key changes?

A

Include the data in the dependency array of the ‘useEffect’ function.

22
Q

In a large-scale production app, what are some better alternatives for loading and managing backend data?

A

React Query and Vercel SWR

23
Q

What is the purpose of React “context”?

A

Data is usually passed from parent component to children components. This can be inconvenient keeping track of props so React created ‘context’ to allow data to be managed by one component but easily shared with multiple components

24
Q

What values can be stored in context?

A

Any data type can be stored in context.

Ex:
userId, theme, language preferences, global state, functions

25
Q

How do you create context and make it available to the components?

A

Create a separate file for context.
1) Create a context by using createContext() and assign it to a const variable
2) Use that context from the component that needs the data using useContext()
3) Provide that context from the component that specifies the data. Wrap with the contextname.provider with a value prop.

26
Q

How do you access the context values?

A

You can access the values stored in context using the ‘useContent’ hook.

27
Q

When would you use context? (In addition to the best answer: “rarely”)

A

Themes such as dark or light.

Many components might need to know the currently logged in user. Putting it in context makes it convenient to read it anywhere in the tree.

For routing. Most routing solutions use context internally to hold the current route. This is how every link “knows” whether it’s active or not. If you build your own router, you might want to do it too.

Managing state.

28
Q

What is a React custom hook?

A

A React custom hook is a JavaScript function that starts with the word “use” and uses one or more built-in hooks or other custom hooks to provide some functionality that can be reused across multiple components in a React application.

Custom hooks are a way to share logic between components without duplicating code or using higher-order components, render props, or mixing. They encapsulate a certain behavior and expose an interface that allows other components to use it.

It is a hook that you write and you can call other hooks in there.

29
Q

When are custom hooks useful? When are they not required?

A

In summary, custom hooks are useful when you need to share functionality between multiple components or abstract complex logic. However, they are not always required and might be overkill for simple functionality or small codebases.

You don’t need to have a custom hook when it doesn’t call another hook.

30
Q

What is the syntax (or naming convention) for writing a custom hook?

A

the hook must start with ‘use’ followed by a capital letter

31
Q

How do you call a custom hook?

A

It’s important to note that custom hooks should only be called at the top level of a functional component or another custom hook. They should not be called inside loops, conditions, or nested functions.

Usually destructured the values.

32
Q

When do custom hooks execute?

A

Custom hooks execute whenever they are called within a component. Like other React hooks, custom hooks run during the rendering phase of a component.

When a custom hook is called, its code is executed from top to bottom. If the custom hook uses other hooks, those hooks will be called as well. React ensures that hooks are called in the same order on every render.

One important thing to note is that each call to a custom hook creates a new instance of the internal state associated with that hook. This means that the state of one instance of the custom hook does not affect the state of another instance of the same hook.