React.js 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

object

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()

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

What is JSX?

A

syntax extension to JavaScript (JS plus some more).

-Transpilation step where JSX gets transpiled into JS

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

In order to use the methods on the react object (ex. react.createElement)

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

Use babel loader and the plugin @babel/plugin-transform-react-jsx

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

What is a React component?

A

a JS function that accepts props as inputs and returns react elements

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 definition or a class, capitalize the names

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

ReactDOM.render()

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

What are props in React?

A
  1. Props is the shorthand for Properties in React.
  2. Props are read-only components which must be kept pure i.e. immutable.
  3. Props are always passed down from the parent to the child components throughout the application.
  4. A child component can never send a prop back to the parent component. This helps in maintaining the unidirectional data flow and are generally used to render the dynamically generated data.
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

as a key=”value” pair inside react element

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

{ }

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

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

A

array.prototype.map

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

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

A

ID number or name from data that distinguishes it from another piece of sibling data

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

How do you create “class” component in React?

A
class ComponentName extends React.Component {
    render() {
    }
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

How do you access props in a class component?

A

this.props

17
Q

What is the purpose of state in React?

A
  1. States are the source of data and must be kept as simple as possible.
  2. States are the objects which determine components rendering and behavior.
  3. States are mutable unlike the props and create dynamic and interactive components.
  4. States are accessed via this.state().
18
Q

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

A

attached as a prop to your react element returned from the render method

19
Q

What are controlled components?

A

form input 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

21
Q

When does React call a component’s componentDidMount method?

A

Right when component is mounted in ReactDOM.render

22
Q

Name three React.Component lifecycle methods.

A

componentDidMount
componentWillUnmount
componentDidUpdate

23
Q

How do you pass data to a child component?

A

by passing the data through the props when rendering/returning the children react element

24
Q

Basic Project Structure for a React Project

A

1) A src folder containing all JS modules
2) Index.html
3) Package.json
4) Module packager or build tool

25
Q

What should the source folder not contain?

A
Static assets that don't go through the module bundler (webpack) e.g. index.html, images and CSS files*
-Instead leave that in a server folder
26
Q

Besides using named props, it’s also possible to reference the content between the opening and closing tag using what?

A

props.children

27
Q

What 3 principles are useful for breaking the interface into nested components?

A
  1. They should be small and have a single concern i.e. do one thing.
  2. Wireframes and understand component hierarchy
  3. Look at the data model - break your UI into components that represent exactly on piece of your data model
28
Q

What are two typical things that might change the state of an application?

A

User interaction and data fetching

29
Q

For performance reasons it’s not viable to trash and re-render the entire interface every time state changes. What’s React’s solution to this?

A

The ‘virtual DOM’ - an in-memory, lightweight representation of the DOM

Faster to manipulate than the real DOM

30
Q

What are the 2 alternatives to using if statements in JSX?

A

Use a ternary operator or take the if statement out of the JSX

31
Q

What are the different phases of React component’s lifecycle?

A
  1. Initial Rendering Phase: This is the phase when the component is about to start its life journey and make its way to the DOM.
  2. Updating Phase: Once the component gets added to the DOM, it can potentially update and re-render only when a prop or state change occurs. That happens only in this phase.
  3. Unmounting Phase: This is the final phase of a component’s life cycle in which the component is destroyed and removed from the DOM.
32
Q

What do you know about controlled and uncontrolled components?

A

Controlled Components

  1. They do not maintain their own state
  2. Data is controlled by the parent component
  3. They take in the current values through props and then notify the changes via callbacks

Uncontrolled Components

  1. They maintain their own state
  2. Data is controlled by the DOM
  3. Refs are used to get their current values