React.js Flashcards

1
Q

What is React?

A

A free and open-source front-end JavaScript library for building interactive user interfaces using declarative language and based on UI components

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

What is a React element?

A

Object - plain JavaScript object that describes the 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 element to the DOM?

A

Create the React element by calling React.createElement()
Query the DOM for the container div element - root
Create a React root by calling ReactDOM.createRoot(container)
Call the render method of the root object to mount the React element

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

What is JSX?

A

JavaScript XML - JavaScript syntax extension, used with React to describe what the UI should look like. JSX converts HTML tags into 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

The JSX gets internally inserted into many React.createElement() function calls and each of them returns an object. Need to import React from “react” since internally every JSX is creating a React Component using JSX transformer.

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

Install webpack, webpack-cli, babel-loader, @babel/core, @babel/plugin-transform-react-jsx as devDependencies and react, react-dom as dependencies
Then “Build” script with npm run

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

What is a React component?

A

Components are conceptually like JavaScript functions or classes. They accept arbitrary inputs (called “props”) and return React elements describing what should appear on the screen.

Components let you split the UI into independent, reusable pieces, and think about each piece in isolation.

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

function Component([props]) {
return textContent {JS expression};
}

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

Define a function Component that returns an element
Query container
root = ReactDOM.createRoot(container)
root.render(Component)

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

Is React a framework or library?

A

React is a FRAMEWORK not a library - INVERSION OF CONTROL. Function itself is being passed as an argument into React.createElement(Component) but it is never called. You define the function but React decides when to call the function you created.

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

What are props in React?

A

Information passed into React components as arguments. Props are objects which can be used inside a component.

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

Can pass props to any component just like how you declare attributes for any HTML tag.

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

Write JavaScript expressions 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

class ClassName extends React.Component {
render() {
return textContent {JS expression “this.props”}
}
}

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

this.props

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

The state is a built-in React object that is used to contain data or information about the component. A component’s state can keep track of change over time; whenever it changes, the component re-renders.

17
Q

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

A

With JSX you pass a function as the event handler propEventListener={function} …
rather than a string like an html attribute=value,

18
Q

WhatArraymethod is commonly used to create a list of React elements?

A

map()

19
Q

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

A

IDs from the data, has to be a unique identifier for the object

20
Q

What are controlled components?

A

Controlled Components are those in which form’s data is handled by the component’s state. It takes its current value through props and makes changes through callbacks like onClick, onChange, etc. A parent component manages its own state and passes the new values as props to the controlled components. With a controlled component, the input’s value is always driven by the React state.

21
Q

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

A

Value and onChange