React.js Flashcards

1
Q

What is React?

A

A front-end framework. It lets you use reusable components and makes it easier to implement interactive elements.

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

What is a React element?

A

A custom written DOM element. React apps are made out of components. A component is a piece of the UI (user interface) that has its own logic and appearance. A component can be as small as a button, or as large as an entire page.

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

How do you mount a React element to the DOM?

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

What is JSX?

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

How does React use JSX to render components?

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

What is a React component?

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

How do you define a component in React?

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

How do you mount a component to the DOM (or “render” a component)?

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

What are props in React?

A

The react equivalent of HTML attributes.

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

How do you use props in a component?

A

Write the name of the props inside curly braces { prop1, prop2 }. Destructure it.

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

How do you pass props to a component?

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

How do you write JavaScript expressions in JSX?

A

Within curly braces { }.

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

How do you pass an event handler to a React component?

A

You pass the prop for the event handler with a value of the event handler prop.
onXxx={handleXxx}

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

What is the naming convention for event handlers?

A

React documentation suggests that Event Handlers be named handleXxx, where Xxx is the event it is handling. This is just a convention, but it aids code clarity.

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

What are some custom event handler props a component may wish to define?

A

onClick, onSubmit, onChange, onMouseEnter,

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

What is the naming convention for custom event handler props?

A

React documentation suggests that Event Handler props be named onXxx, where Xxx is the event. This is just a convention, but it aids code clarity.

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

What are hooks in React?

A

Hooks are special functions that are only available while React is rendering. They let you “hook into” different React features. They all start with “use” followed by an uppercase letter.

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

What are the “Rules of Hooks”?

A

Only call hooks at the top level, the first line after your react function and cannot be inside loops, conditions, or nested functions.
Only call hooks from react components or from other hooks.
Must start with “use” then an uppercase letter e.g. “useHook”

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

What is the purpose of state in React?

A
  1. A state variable to retain the data between renders.
  2. A state setter function to update the variable and trigger React to render the component again.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
20
Q

Why can’t we just maintain state in a local variable?

A
  1. Local variables don’t persist between renders. When React renders this component a second time, it renders it from scratch—it doesn’t consider any changes to the local variables.
  2. Changes to local variables won’t trigger renders. React doesn’t realize it needs to render the component again with the new data.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
21
Q

What two actions happen when you call a state setter function?

A
  1. It renders the component again.
  2. Remembers the latest state value.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
22
Q

When does the local state variable get updated with the new value?

A

The next time the component would render. On the re-render. The next time useState is called.

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

How do controlled components differ from uncontrolled components?

A

Controlled components are controlled by the parent, e.g. the form while uncontrolled components are controlled by the child, e.g. the input element.

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

What are some advantages of using uncontrolled components?

A

You don’t have to keep track of state with useState and just get the form data instead of each state variable.

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

What are some advantages of using controlled components?

A
26
Q

Which style do you prefer?

A

Controlled components.

27
Q

What two props must you pass to an input for it to be “controlled”?

A

value/checked and onChange.

28
Q

What are some popular npm packages for creating forms in React?

A

React Hook Form, Formik, and React Final Form.

29
Q

When would we want to create a list of React components?

A

When you want to display multiple similar components from a collection of data.

30
Q

Why is it important for React components to be data-driven?

A

So that the data is the only thing that changes and the components can be used for any set of data.

31
Q

Where in the component code would a list of React components typically be built?

A

Before the return of the jsx.

32
Q

What Array method is commonly used to create a list of React components?

A

map(), and also filter() then map().

33
Q

Why do components in a list need to have unique keys?

A

To uniquely identify it. Keys tell React which array item each component corresponds to, so that it can match them up later. This becomes important if your array items can move (e.g. due to sorting), get inserted, or get deleted. A well-chosen key helps React infer what exactly has happened, and make the correct updates to the DOM tree.

34
Q

What is the best value to use as a “key” prop when rendering lists?

A
  1. Data from a database: If your data is coming from a database, you can use the database keys/IDs, which are unique by nature.
  2. Locally generated data: If your data is generated and persisted locally (e.g. notes in a note-taking app), use an incrementing counter, crypto.randomUUID() or a package like uuid when creating items.
35
Q

What are two ways React components can interact?

A
36
Q

How can multiple React components share state?

A
37
Q

What are the steps to “lift state up”?

A
38
Q

When would you want to lift state up?

A
39
Q

When is a component “mounted” to the DOM?

A

When is appears on the page for the first time.

40
Q

What is a React Effect?

A

Effects let you run some code after rendering so that you can synchronize your component with some system outside of React.

41
Q

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

A

Effects let you specify side effects that are caused by rendering itself, rather than by a particular event.

42
Q

When do Effects run?

A

By default, Effects run after every render. You can tell React to skip unnecessarily re-running the Effect by specifying an array of dependencies as the second argument to the useEffect call. The dependency array can contain multiple dependencies. React will only skip re-running the Effect if all of the dependencies you specify have exactly the same values as they had during the previous render.

43
Q

What function is used to declare an Effect?

A

To declare an Effect in your component, import the useEffect Hook from React:

import { useEffect } from ‘react’;

44
Q

What are Effect dependencies and how do you declare them?

A

You can tell React to skip unnecessarily re-running the Effect by specifying an array of dependencies as the second argument to the useEffect call. The dependency array can contain multiple dependencies. React will only skip re-running the Effect if all of the dependencies you specify have exactly the same values as they had during the previous render.

45
Q

Why would you want to clean up from an Effect?

A

Release resources you have allocated in your useEffect function, to avoid memory leaks.

46
Q

How do you clean up from an Effect?

A

By returning a cleanup function from your Effect.

47
Q

When does the cleanup function run?

A

Before the cleanup function runs.

48
Q

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

A

fetch after mount

49
Q

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

A

fetch

50
Q

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

A

Have an empty dependency array.

51
Q

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

A

Have the data key in the dependency array.

52
Q

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

A

You manage all your data accesses with a third-party data management library. Some good open source packages are React Query and Vercel SWR work.

53
Q

What is the purpose of React “context”?

A

Context lets the parent component make some information available to any component in the tree below it—no matter how deep—without passing it explicitly through props.

54
Q

What values can be stored in context?

A

Anything, from variables to functions in the hierarchy. Context is not limited to static values. If you pass a different value on the next render, React will update all the components reading it below! This is why context is often used in combination with state.

55
Q

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

A

const context = createContext(defaultValue)

56
Q

How do you access the context values?

A

Call useContext at the top level of your component to read and subscribe to context.

57
Q

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

A

Theming: If your app lets the user change its appearance (e.g. dark mode), you can put a context provider at the top of your app, and use that context in components that need to adjust their visual look.
Current account: 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. Some apps also let you operate multiple accounts at the same time (e.g. to leave a comment as a different user). In those cases, it can be convenient to wrap a part of the UI into a nested provider with a different current account value.
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: As your app grows, you might end up with a lot of state closer to the top of your app. Many distant components below may want to change it. It is common to use a reducer together with context to manage complex state and pass it down to distant components without too much hassle.

In general, if some information is needed by distant components in different parts of the tree, it’s a good indication that context will help you.

58
Q

What is a React custom hook?

A

A hook that you write that can call other hooks.

59
Q

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

A

When you want to avoid duplicate code or to clean up duplicate code that makes code less readable. They are not required if they don’t call other hooks and are not needed if the code is used a couple times.

60
Q

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

A
  1. React component names must start with a capital letter, like StatusBar and SaveButton. React components also need to return something that React knows how to display, like a piece of JSX.
  2. Hook names must start with use followed by a capital letter, like useState (built-in) or useOnlineStatus (custom, like earlier on the page). Hooks may return arbitrary values.
61
Q

How do you call a custom hook?

A

The same way you call built in hooks. They must be at the top level and cannot be called conditionally.

62
Q

When do custom hooks execute?

A

When they are called.