React Flashcards

1
Q

What is React?

A

A JavaScript library for building user interfaces

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

What is a React element?

A

It is the basic building block in a react application, it is an object representation of a virtual DOM node. React Element contains both type and property

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

const create = React.createElement(
‘h1’,
null,
‘Hello, React!’
);

const container = document.querySelector(‘#root’);
ReactDOM.createRoot(container).render(create);

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

What is a React component?

A

a React component is a JavaScript function that you can sprinkle with markup.

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

How do you define a function component in React?

A

With function Profile() { } you define a JavaScript function with the name Profile.

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

How do you mount a component to the DOM?

A

const container = document.querySelector(‘#root’);
const root = ReactDOM.createRoot(container);
root.render(variable);

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

What are props in React?

A

standardized way to pass arguments

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

How do you pass props to a component?

A

list properties

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

How do you write JavaScript expressions in JSX?

A

curly brackets

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

What is the purpose of state in React?

A

A state variable to retain the data between renders.
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
11
Q

How to you pass an event handler to a React element?

A

<button onClick={handleClick}>{props.text}</button>;

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

What are controlled components?

A

An input like <input></input> is uncontrolled. Even if you pass an initial value like <input></input>, your JSX only specifies the initial value. It does not control what the value should be right now.

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

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

A

value, onChange

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

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

A

filter() and map()

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

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

A

something unique (don’t use array index - order may change)
Data from a database: If your data is coming from a database, you can use the database keys/IDs, which are unique by nature.

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.

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

What is a React Effect?

A

function that will execute in special cases

17
Q

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

A

when you have actions that need to be executed outside of rendered user actions/events

18
Q

When do Effects run?

A

1) after mount/render
2) when dependency changes

19
Q

What function is used to declare an Effect?

A

useEffect
parameters: callback function, dependency array

20
Q

What are Effect dependencies and how do you declare them?

A
21
Q

How do you clean up from an Effect?

A

returning a function from effect callback

22
Q

When does the clean up function run?

A

1) before the unmount
2) runs when things change and before it does, cleans up

23
Q

When does React call a component’s componentDidMount method?

A

This method is called during the Mounting phase of the React Life-cycle i.e after the component is rendered.

24
Q

Name three React.Component lifecycle methods.

A

Mounting, Updating, and Unmounting.

25
Q

How do you pass data to a child component?

A

you do this by using props.