ReactJS Flashcards

1
Q
  1. How does React update the DOM?
A

Once the virtual DOM has been updated, then React compares the virtual DOM with a virtual DOM snapshot that was taken right before the update.

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

How is a DOM represented?

A

The DOM is a tree-like representation of the web page that gets loaded into the browser.
It represents the web page using a‌‌ series of objects. The main object is the document object.

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

How do you utilize multiple pages in React?

A

One approach is to utilize the React Router library. React Router manages the browser history and allows components to be displayed on specific pages corresponding to specific URLs. Enclose all pages in the Router and routes component along with their path.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q
  1. Describe how to use useState.
A

The useState() hook takes the first (initial) value of the state variable as its argument. The second value then sets your state, which is why it’s always initiated as setState.

The useState hook is used in React components to manage state. It lets you generate and read a state variable, as well as update the state variable.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q
  1. What happens with a component when it receives new props?
A

By default, when your component’s state or props change, your component will re-render.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q
  1. What is ReactJS?
A

It is an open-source front-end JavaScript library that is used for building composable user interfaces, especially for single-page applications.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q
  1. What are the major features of React?
A
  • Uses JSX syntax, a syntax extension of JavaScript that allows developers to write HTML in their JS code.
  • It uses Virtual DOM instead of Real DOM considering that Real DOM manipulations are expensive.
  • Supports server-side rendering which is useful for Search Engine Optimizations(SEO).
  • Follows Unidirectional or one-way data flow or data binding.
  • Uses reusable/composable UI components to develop the view.
  • React updates only those components that have changed, rather than updating all the components at once. This results in much faster web applications.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q
  1. What is JSX?
A

JSX is a syntax extension of JavaScript. It is used with React to describe what the user interface should look like. By using JSX, we can write HTML structures in the same file that contains JavaScript code.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q
  1. What is the difference between Element and Component?
A

An Element is a plain object describing what you want to appear on the screen in terms of the DOM nodes or other components. Elements can contain other Elements in their props. Creating a React element is cheap. Once an element is created, it cannot be mutated.
The JavaScript representation(Without JSX) of React Element would be as follows:

const element = React.createElement(“div”, { id: “login-btn” }, “Login”);

and this element can be simiplified using JSX

<div>Login</div>

Whereas a component can be declared in several different ways. It can be a class with a render() method or it can be defined as a function. In either case, it takes props as an input, and returns a JSX tree as the output:

const Button = ({ handleLogin }) => (
<div id={“login-btn”} onClick={handleLogin}>
Login
</div>
);

Then JSX gets transpiled to a React.createElement() function tree:

const Button = ({ handleLogin }) =>
React.createElement(
“div”,
{ id: “login-btn”, onClick: handleLogin },
“Login”
);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q
  1. How to create components in React?
A

Components are the building blocks of creating User Interfaces(UI) in React. There are two possible ways to create a component.

  • Function Components: This is the simplest way to create a component. Those are pure JavaScript functions that accept props object as the first parameter and return React elements to render the output:
function Greeting({ message }) {
  • return <h1>{Hello, ${message}}</h1>;
  • }


  • Class Components: You can also use ES6 class to define a component. The above function component can be written as a class component:
class Greeting extends React.Component {
  • render() {
  • return <h1>{Hello, ${this.props.message}}</h1>;
  • }
  • }
How well did you know this?
1
Not at all
2
3
4
5
Perfectly