React.js Flashcards

1
Q

What is React.js?

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 an object that describes what the actualDocument Object Model(DOM)element should look like.

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

What does the createElement( ) method do?

A

It creates & returns a new React element of the given type.

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 that’s generally used with React to describe what the DOM should look like. It looks like an HTML tag.

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 toReact.createElement from Babel, theReactlibrary must also always be in scope from your JSX code.

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

What is a React component?

A

Components areindependent and reusable bits of code. They serve the same purpose as JavaScript functions, but work in isolation and return HTML. 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
7
Q

How do you define a function component in React?

A

First, you define a function with a name (first letter of name should be capitalized). Then we have a parameter called props that represent the property. Inside the code block we have code that returns JSX code.

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?

A

By using the render method of the ReactDOM object.

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

How you install React & ReactDOM as a dependency?

A

npm install react + npm install react-dom

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

Fill in the blank.

Fundamentally, JSX just provides _____ for the React.createElement( ) function.

A

Syntactic sugar

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

How do you put a JavaScript expression in JSX?

A

You put a JavaScript expression inside the curly braces in JSX inside the JSX tags.

For example:

function getGreeting(user) {
  if (user) {
    return <h1> Hello, {formatName(user)}! </h1>;
  }
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

How should you name React elements and why should name it that way?

A

First letter should be uppercased or else React will think of it as an HTML tag and it will be converted as a string from Babel.

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

What are props in React?

A

Props arearguments passed into React components. Props (which stands for properties) is an object and it contains all of thepropspassed to the child component.

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

How do you pass props to a component?

A

You create a react element in the component, then assign the prop name to a value.

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

How do you write JavaScript expressions in JSX?

A

You can write any JavaScript expression by surrounding it with{ }.

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

In React, how is code reuse primarily achieved?

A

Code reuse in React is primarily achieved through composition rather than inheritance.

17
Q

What is composition?

A

Composition is a structure where everything in JavaScript is treated as an object—even functions in JavaScript are treated as a high-class object. Such objects are quite complex in nature to make large complex objects simple, many small objects are composed together.

18
Q

What is inheritance?

A

Inheritance is gaining some or all traits (functionality) of parent and then providing a relational structure.

19
Q

How do you create “class” component in React?

A

You need to define a class that extends React.Component. Then define a render ( ) method returning a React element.

20
Q

How do you access props in a class component?

A

By using this.props.

21
Q

What is render ( ) and is it being called or defined in a React class component?

A

Render( ) is a prototype method that is supposed to render the returned React element. The method is defined in a class component.

22
Q

Why is render ( ) being defined considering it looks like a function call with a code block?

A

This is syntactic sugar for a property name and its value. So render ( ) is creating a property called render on the class with a value of render ( ).

23
Q

What is the purpose of state in React?

A

Since state is managed only within the component, the purpose of state is to represent information about the component’s current situation. In other words, it keeps track of values that change over time

24
Q

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

A

You pass an event handler to a React element by setting the prop of the react element to onClick (or whatever the event is) with its value inside a curly bracket.

25
Q

What is the difference between props and state?

A

They’re both plain JavaScript objects, but State is private and fully controlled by the component.

26
Q

What does the setState( ) method do?

A

Sends a request to React that this component and its children need to be re-rendered with the updated state. This is the primary method you use to update the user interface in response to event handlers and server responses.

27
Q

What is a key in React?

A

A “key” is a special string attribute you need to include when creating lists of elements. It helps React identify which items have changed, are added, or are removed.

28
Q

What is the best way to pick a key?

A

Use a string that uniquely identifies a list item among its siblings. Most often you would use IDs from your data as keys.

29
Q

What happens when you choose not to assign an explicit key to list items?

A

React will default to using indexes as keys.

30
Q

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

A

Array.map ( )

31
Q

What are controlled components?

A

An input form element whose state and value is controlled by React.

32
Q

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

A

value & onChange