react js Flashcards

1
Q

What is react

A

React is a declarative, efficient, and flexible JavaScript library for building user interfaces. It lets you compose complex UIs from small and isolated pieces of code called “components”.

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

React Virtual DOM

A

DOM is basically the representation of the HTML code in a webpage. The document is the webpage itself, the objects are the HTML tags. And finally, the model of DOM is a tree structure:

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

What are the 3 benefit of Virtual DOM?

A

Each time you make a change in the code, DOM will be completely updated and rewritten. This is an expensive operation and consumes lots of time. In this point, React provides a solution: The Virtual DOM.

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

So when something changes:

A

1) React first creates an exact copy of the DOM
2) Then React figures out which part is new and only updates that specific part in the Virtual DOM3)Finally, React copies only the new parts of the Virtual DOM to the actual DOM, rather than completely rewriting it.

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

What is JSX

A

JSX (JavaScript XML) is a syntax extension to JavaScript used by React. JSX is basically used to write HTML tags inside JavaScript. Later, the JSX code will be translated into normal JavaScript, by Babel.

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

JSX class

A

className

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

Command to setup React scaffolding

A

npx create-react-app my-app

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

What is a React Component?

A

A component is an independent, reusable code block, which divides the UI into smaller pieces. In other words, we can think of components as LEGO blocks.

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

What are React 2 types of components

A

1) Functional (Stateless)

2) Class (Stateful).

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

Describe Functional (Stateless) Components

A

A functional component is basically a JavaScript (or ES6) function which returns a React element.

example: function Welcome(props) {
return <h1>Hello, {props.name}</h1>;
}

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

So a React Functional Component: has these 3 things

A

1) is a JavaScript / ES6 function
2) must return a React element
3) take props as parameter if necessary

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

what is a Class (Stateful) Components

A

Class components are ES6 classes. They are more complex than functional components including constructors, life-cycle methods, render( ) function and state (data) management.

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

4 items React class component:

A

1) is an ES6 class, will be a component once it ‘extends’ React component.
2) can accept props (in the constructor) if needed
3) can maintain its own data with state
4 must have a render( ) method which returns a React element (JSX), or null

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

Create parentComponent file

A

import React, { Component } from ‘react’;

class ParentComponent extends Component {
  render() {
    return <h1>I'm the parent component.</h1>;
  }
}
export default ParentComponent;
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Create firstChild file

A
import React from 'react';     // React needs to be imported
const FirstChild = () => {
  return <p>I'm the 1st child!</p>; 
};
export default FirstChild;
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

How to call a component?

A

A component is being called like an HTML tag, but starting with a capital letter:

// we can call a component like an HTML tag
17
Q

What are Props?

A

React is a component-based library which divides the UI into little reusable pieces. In some cases, those components need to communicate (send data to each other) and the way to pass data between components is by using props.

18
Q

4 items for Props

A

1) Props stand for properties and is a special keyword in React
2) Props are being passed to components like function arguments
3) Props can only be passed to components in one-way (parent to child)
4) Props data is immutable (read-only)

19
Q

Read data(textMe) in a prop

A
const ChildComponent = (props) => {  
  return <p>{props.textMe}</p>; 
}
20
Q

Get data from prop

A
21
Q

React State 5 items

A

1) State is a special object that holds dynamic data
2) State is private and belongs only to its component where defined, cannot be accessed from outside
3) State is initialized inside its component’s constructor method.
4) Update with setState( ) method
5) State should not be overly-used in order to prevent performance problems

22
Q

Since state is an object, it should be initialized inside the constructor method: give exampled

A
constructor() {
  this.state = {
    id: 1,
    name: "test"
  };
}
23
Q

Updating The State

A

this.setState({
name: “testing state”
});

24
Q

Only way to update State

A

setState()