React Flashcards

1
Q

What is React?

A

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

Plain object describing a component instance or DOM node and its desired properties.

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

ReactDOM.render(element, container, [callback]);

container is where you want to add the element.

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

What is JSX?

A

a syntax extension to JavaScript; produces React “elements”; fundamentally, JSX just provides syntactic sugar for the React.createElement(component, props, …children) function.

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

Since JSX compiles into calls to React.createElement, the React library must also always be in scope from your JSX code.

Babel transform it into create call, will be undefined???

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

Babel compiles JSX down to React.createElement() calls.

Use babel-loader and @babel/plugin-transform-react-jsx packages.

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

What is a React component?

A

Takes input and returns a React element

Components let you split the UI into independent, reusable pieces, and think about each piece in isolation. Conceptually, components are like JavaScript functions.

They accept arbitrary inputs (called “props”) and return React elements describing what should appear on the screen.

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

First letter has to be capitalized, has to return a react element

function Welcome(props) {
  return &lth1>Hello, {props.name}&lt/h1>;
}
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
function Welcome(props) {
  return &lth1>Hello, {props.name}&lt/h1>;
}

const element = &ltWelcome name=”Sara” />;
ReactDOM.render(
element, document.getElementById(‘root’)
);

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

What are props in React?

A

object passed into a React component

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
const element = (
    &ltComponentName property-name="string" /&gt
    &ltComponentName property-name={1 + 2} /&gt
);
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

Inside curly brackets {}

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

How do you create “class” component in React?

A

To define a React component class, you need to extend React.Component. The only method you must define in a React.Component subclass is called render(). All the other methods described on this page are optional.

class Welcome extends React.Component {
   render() {
     return &lth1&gtHello, {this.props.name}&lt/h1>
   }
 }
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

How do you access props in a class component?

A

use ‘this’ object

this.props.name

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

What is the purpose of state in React?

A

State is data model in react; used in render logic to determine what the user should see; to keep of track of values that change over time

State is similar to props, but it is private and fully controlled by the component.

Store property values belonging to component

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

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

A

Pass a prop in react element?

&ltbutton onClick={activateLasers}&gt
Activate Lasers
&lt/button&gt

pass function in curly bracket {}

17
Q

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

A

map() method

18
Q

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

A

A string uniquely to identify each sibling preferably an id

Use a string that uniquely identifies a list item among its siblings. Most often you would use IDs from your data as keys. When you don’t have stable IDs for rendered items, you may use the item index as a key as a last resort

19
Q

What are controlled components?

A

An input form element whose value is controlled by React

20
Q

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

A

value and onChange