JavaScript React Flashcards

1
Q

What is React?

A

a framework that makes it easier to make reusable components. To make interactive code easier to write.

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

What is a React element?

A

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. A custom written DOM element.

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

How do you mount a React component to the DOM? or
How do you insert/render a component to the DOM tree?

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

What is JSX?

A

JSX stands for JavaScript XML.
JSX allows us to write HTML in React.
JSX makes it easier to write and add HTML in React.

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

Reusable UI
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. A custom written DOM element.

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

a function whose name starts with a capital letter. the return should be JSX.

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

attach it to the root. calls the function when it sees it wrapped in <>. Takes the result of calling the function and puts it in the DOM.

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

What are props in React?

A

“Props” is a special keyword in React, which stands for properties and is being used for passing data from one component to another. But the important part here is that data with props are being passed in a uni-directional flow. ( one way from parent to child). Corresponds to attributes in HTML.

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

destructure the props object in the parameter. or use the props object as a parameter.

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

in angle brackets, prop propertyname = value
in curly braces, call the component with arguments of values of the props.

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

You write JavaScript expressions in JSX inside curly braces. {JAVASCRIPT EXPRESSION}

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

pass your event handler as a prop to your component

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

handleEvent() {

}

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, onMouseLeave,

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

onEventHandlerProps

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 start with use followed by an uppercase letter. ie useState.

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

What are the “Rules of Hooks”? (if necessary, re-read the “Pitfall” box in State)

A

Hooks—functions starting with use—can only be called at the top level of your components or your own Hooks. You can’t call Hooks inside conditions, loops, or other nested functions. You can only call them in react components and other hooks.

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

to update a local data.
state holds the data inbetween function calls.

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

it exists only within the block that it is declared in. Once that block ends, the variable is destroyed and its values lost”.

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

triggers react to rerender again. updates a cashe of the state variable.

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

on the re render.

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 parent is in control.???
uncontrolled components are controlled by the input itself.???

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 dont have to use useState. It is simpler, you can just grab the form data.

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

Ui and data are in sync, when you update the UI, the data is updated at the same time. Anytime you have 2 components interacting with each other is a good time to use it.???

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

Which style do you prefer?

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

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

A

value and onChange

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

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

A

The 3 most popular packages are React Hook Form, Formik, and React Final Form. React Hook Form is lightweight, performant, and easy to use, but it requires a version of React that supports hooks (v16.8 or higher). Since React hooks are fairly ubiquitous at this point, React Hook Form is an excellent choice. Formik handles complex forms well but is large and complex to use.

29
Q

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

A

When you want to make a list based off data.

30
Q

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

A

So that the only thing that changes is the data, and the markup looks the same

31
Q

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

A

inside the function body

32
Q

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

A

arrray.map()

33
Q

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

A

react needs know which is which.

34
Q

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

A

most of the time your data will have an id. so use that id.

35
Q

What are two ways React components can interact?

A

The two most common are by passing properties from a parent component to children components, and by responding to Events from children components.

36
Q

How can multiple React components share state?

A

Sometimes, you want the state of two components to always change together. To do it, remove state from both of them, move it to their closest common parent, and then pass it down to them via props. This is known as lifting state up, and it’s one of the most common things you will do writing React code.

37
Q

What are the steps to “lift state up”?

A
  1. Remove state from the child components.
  2. Pass hardcoded data from the common parent.
  3. Add state to the common parent and pass it down together with the event handlers.
38
Q

When would you want to lift state up?

A
39
Q

When is a component “mounted” to the DOM?

A

When the component appears on the page for the first time

40
Q

What is a React Effect?

A

block of code that runs after the page is rendered

41
Q

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

A

You do need Effects to synchronize with external systems. You don’t need Effects to transform data for rendering. You don’t need Effects to handle user events.
if all youre doing is updating state, dont use effect.

42
Q

When do Effects run?

A

after updating the DOM

43
Q

What function is used to declare an Effect?

A

useEffect

44
Q

What are Effect dependencies and how do you declare them?

A

used to tell react when an effect should run. declare dependencies in the second argument of the useEffect callback in an array.

45
Q

Why would you want to clean up from an Effect?

A

Cleaning up side effects in React is a way of stopping side effects that do not need to be executed anymore. Release resources that you have allocated. To avoid memory leaks.

46
Q

How do you clean up from an Effect?

A

return;

47
Q

When does the cleanup function run?

A

While it’s commonly known that the cleanup function runs when a component unmounts, it’s less commonly known that the cleanup function also runs after each re-render.

48
Q

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

A
49
Q

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

A

Fetch API

50
Q

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

A

empty array as second parameter

51
Q

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

A

type in the data key in an array as a second parameter

52
Q

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

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 a parent component provide data to the entire tree below it.

54
Q

What values can be stored in context?

A

any value

55
Q

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

A
  1. Create a context. (You can call it LevelContext, since it’s for the heading level.)
  2. Use that context from the component that needs the data. (Heading will use LevelContext.)
  3. Provide that context from the component that specifies the data. (Section will provide LevelContext.)
    const variable = createContext()
56
Q

How do you access the context values?

A

useContext

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.

Anytime you want to share data to alot of components and dont want to pass props all the way down a tree.

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 dont want to duplicate code. You don’t need a custom hook when it doesn’t call another hook.

60
Q

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

A

Custom Hooks must be named starting with use followed by a capital letter.

61
Q

How do you call a custom hook?

A

Same why you call a regular hook. Call at the top level which means it cannot be conditionally called.

62
Q

When do custom hooks execute?

A

All Hooks re-run every time your component re-renders or when they’re called.

63
Q

In JavaScript, when is scope determined?

A

happens at parse time.

64
Q

What allows JavaScript functions to “remember” values from their surroundings?

A

Closure

65
Q

What values does a closure contain?

A

All variables and function that are in it’s parents scope that is used by the function.

66
Q

When is a closure created?

A

Every time a function reference is created which is at runtime.

67
Q

How can you tell if a function will be created as a closure?

A

Anytime a function needs to use variables that are outside its scope.

68
Q

In React, what is one important case where you need to know if a closure was created?

A

When there’s a useEffect,
if you have a function that is a dependency in your useEffect, you have to care if a closure was created

69
Q

What is Closure?

A

The key to remember is that when a function gets declared, it contains a function definition and a closure. The closure is a collection of all the variables in scope at the time of creation of the function.