React Flashcards

1
Q

What is React?

A

React is a JavaScript library for creating 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

A React element is an object representation that describes the DOM element.
A representation of a DOM node.

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

Use the render method of the root object and pass the React element as an argument
ex.
import React from ‘react’;
import ReactDOM from ‘react-dom/client’;

const reactElement = React.createElement(
‘h1’,
null,
‘Hello, React!’
);
console.log(reactElement);

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

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

What is JSX?

A

JSX is a syntax extension to JavaScript and you use it to write React “elements”.

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

Why must the React object be imported when authoring JSX in a module?

A

JavaScript will not understand JSX so it must be converted to React elements (React.createElement) using babel.

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

How can you make Webpack and Babel work together to convert JSX into valid JavaScript?

A

Webpack is the bundler.
Babel is the compiler.
These 2 work together with the babel loader, and then the babel plugin is used to transform JSX code into JavaScript
devDependencies:
- webpack
- webpack-cli
- babel-loader
- @babel/core
- @babel/plugin-transform-react-jsx
Dependencies:
- react
- react-dom

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

What is a React component?

A

Conceptually, components are like JavaScript functions. They accept arbitrary inputs (called “props”) and return React elements describing what should appear on the screen.

They can either be written as a function:
function Welcome(props) {
return <h1>Hello, {props.name}</h1>;
}

or as a class:
class Welcome extends React.Component {
render() {
return <h1>Hello, {this.props.name}</h1>;
}
}

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

How do you define a function component in React?

A

Write it like a JavaScript function or class:
function Welcome(props) {
return <h1>Hello, {props.name}</h1>;
}

Rule 1: It’s a JavaScript function
Rule 2: Has to return a react element
Rule 3: Name of the component has to start with a capital letter

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

How do you mount a component to the DOM?

A

Use the render method of the root object.
Ex.
function Welcome(props) {
return <h1>Hello, {props.name}</h1>;
}

const root = ReactDOM.createRoot(document.getElementById(‘root’));
const element = ;
root.render(element);

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

Why is react a framework?

A

The definitions of the two words are that a library is a collection of functions, and a framework is a way of doing things.

By this definition, React is a framework because it forces you to build UI in the React way as opposed to the Angular or Ember, etc. way.

On the other hand lodash is a perfect example of a library because it’s just a collection of functions, use them however you like.

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

props is an object

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

How do you pass props to a component?

A

You specify the props by adding them as key value pairs.
ex.
< CustomButton text=”React!” / >

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

How do you write JavaScript expressions in JSX?

A

They need to be written within curly braces

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

How do you create “class” component in React?

A

When creating a React component, the component’s name must start with an upper case letter.

The component has to include the extends React.Component statement, this statement creates an inheritance to React.Component, and gives your component access to React.Component’s functions.

The component also requires a render() method, this method returns HTML.

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

How do you access props in a class component?

A

Through the “this” object and the props property
ex.
class Welcome extends React.Component {
render() {
return <h1>Hello, {this.props.name}</h1>;
}
}

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

What is the purpose of state in React?

A

State allows us to manage changing data in an application.
The purpose of state is to keep track of values that change over time.

17
Q

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

A

You pass it as a prop into the react element with the JavaScript expression this.___.
Ex.
return < button onClick={ this.handleClick } >Click me!< /button >;

18
Q

What does bind do?

A

Bind guarantees the value of this WHEN the function or method is eventually called.
Bind returns a copy of a function with a permanent this
Ex.
this.handleClick = this.handleClick.bind(this);

19
Q

What does react do when working in the DOM?

A
  1. React deletes everything from the container DOM element and OWNS it thereafter
  2. React looks at your react element and decides what to do
    • If the react element is an html type (button, div, h1) => DOM creation
    • if the react element is a fn component type –> call it (and use the elements)
    • if the react element is a class component type -> NEW (and call render)
      (third option - class)
      react immediately calls the render method of the new object

{
type: “button”,
props: {
onClick: fn,
children: “Click Me!”
}
}
- React does the DOM creation to make the real DOM match the above object and also adds the event listener (for “click”)
- When our click listener runs, it calls setState
- This starts a transition from the current state to the next state by replacing the old property in this.state
- a re-render is scheduled (with the new state)
- react receives a NEW react element from the render method
{
type: “button”,
props: {
children: Thanks!
}
}

20
Q

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

A

Array.map() method

21
Q

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

A

A unique id

22
Q

What are controlled components?

A

Controlled components in React are those in which form data is handled by the component’s state.

23
Q

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

A
  1. Value: To set this.value to the input
  2. onChange: To listen for when the value changes
24
Q

id and label values are connected?

A

yes

25
Q

When does react trigger a re render?

A

When setState is called