React Flashcards

1
Q

What is React?

A

React is a front-end JavaScript library that is used for building user interfaces, especially for single-page applications. It is used for handling view layer for web and mobile apps.

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

What are the major features of React?

A

The major features of React are:

  • It uses VirtualDOM instead of RealDOM considering that RealDOM manipulations are expensive.
  • Supports server-side rendering.
  • Follows Unidirectional data flow or data binding.
  • Uses reusable/composable UI components to develop the view.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What is JSX?

A

It provides syntactic sugar for the React.createElement() function. It’s used with React to describe what the UI should look like. JSX produces React “elements”. JSX is the return mark up output for class and function components.

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

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.

The object representation of React Element would be as follows:

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

The above React.createElement() function returns an object:

{
  type: 'div',
  props: {
    children: 'Login',
    id: 'login-btn'
  }
}
Elements are what components are “made of”
A component can be a class with a render() method or it can be defined as a function. It takes props as an input, and returns a JSX tree as the output.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

How to create components in React?

A

Function Components: pure JavaScript functions that accept props object as the first parameter and return React elements.

Class Components: You can also use ES6 class to define a component.

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

What are Pure Components?

A

React.PureComponent is exactly the same as React.Component except that it handles the shouldComponentUpdate() method for you. When props or state changes, PureComponent will do a shallow comparison on both props and state.

Component won’t compare current props and state. The component will re-render by default whenever shouldComponentUpdate is called.

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

What is state in React?

A

State of a component holds data that may change over the lifetime of the component.

State is private and fully controlled by the component ,i.e., it is not accessible to any other component until the owner component decides to pass it.

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

What are props in React?

A

Props are inputs to components. They are data passed down from a parent component to a child component.

The primary purpose of props in React is to provide following component functionality:

  • Pass custom data to your component.
  • Inform about state changes.
  • Use via props.reactProp inside component’s render() method.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What is the difference between state and props?

A

Both props and state are plain JavaScript objects. Both of them hold information that influences the output of render.

Props get passed TO the component similar to function parameters and are read-only

State is managed WITHIN the component similar to variables declared within a function.

Props are a way of passing data from parent to child. State is reserved only for interactivity, that is, data that changes over time.

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

Why should we not update the state directly?

A

If you try to update the state directly then it won’t re-render the component.

Instead use setState() method. It schedules an update to a component’s state object. When state changes, the component responds by re-rendering.

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

What is the purpose of callback function as an argument of setState()?

A

The callback function is invoked when setState finished and the component gets rendered. Since setState() is asynchronous the callback function is used for any post action.

Note: It is recommended to use lifecycle method rather than this callback function.

setState({ name: ‘John’ }, () => console.log(‘The name has updated and component re-rendered’))

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

What is the difference between HTML and React event handling?

A

Below are some of the main differences between HTML and React event handling,

  • In HTML, the event name usually represents in lowercase as a convention.

Whereas in React it follows camelCase convention.

  • In HTML, you can return false to prevent default behavior

Whereas in React you must call preventDefault() explicitly

  • In HTML, you need to invoke the function by appending () Whereas in react you should not append () with the function name.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

How to bind methods or event handlers in JSX callbacks?

A

There are 3 possible ways to achieve this:

  • Binding in Constructor: In JavaScript classes, the methods are not bound by default. The same thing applies for React event handlers defined as class methods. Normally we bind them in constructor.
  • Public class fields syntax: If you don’t like to use bind approach then public class fields syntax can be used to correctly bind callbacks.
  • Arrow functions in callbacks: You can use arrow functions directly in the callbacks.

Note: If the callback is passed as prop to child components, those components might do an extra re-rendering.

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

JSX Represents Objects

A

Babel compiles JSX down to React.createElement() calls.
React.createElement() performs a few checks to help you write bug-free code but essentially it creates an object

These objects are called “React elements”. You can think of them as descriptions of what you want to see on the screen.

React reads these objects and uses them to construct the DOM and keep it up to date.

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

What are react elements?

A

Elements are the smallest building blocks of React apps.

An element describes what you want to see on the screen

Unlike browser DOM elements, React elements are plain objects, and are cheap to create. React DOM takes care of updating the DOM to match the React elements.

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

What are components?

A

Components let you split the UI into independent, reusable pieces, and think about each piece in isolation.

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

All React components must act like pure functions with respect to their props.

17
Q

Why are keys used with lists?

A

Keys help React identify which items have changed, are added, or are removed.

Keys should be given to the elements inside the array to give the elements a stable identity.

A good rule of thumb is that elements inside the map() call need keys.

Keys used within arrays should be unique among their siblings. However, they don’t need to be globally unique.

18
Q

What is a controlled component?

A

In HTML, form elements such as , , and typically maintain their own state and update it based on user input.

In React, mutable state is typically kept in the state property of components, and only updated with setState().

We can combine the two by making the React state be the “single source of truth”. Then the React component that renders a form also controls what happens in that form on subsequent user input.

With a controlled component, the input’s value is always driven by the React state. You can now pass the value to other UI elements too, or reset it from other event handlers.

19
Q

Explain “single source of truth”

A

There should be a single “source of truth” for any data that changes in a React application. Usually, the state is first added to the component that needs it for rendering.

Then, if other components also need it, you can lift it up to their closest common ancestor.

Instead of trying to sync the state between different components, you should rely on the top-down data flow.

20
Q

Rules of JSX

A
  1. Return a single root element

To return multiple elements from a component, wrap them with a single parent tag.

  1. Close all the tags

JSX requires tags to be explicitly closed: self-closing tags like <img></img> must become <img></img>, and wrapping tags like <li>oranges must be written as </li><li>oranges</li>.

  1. camelCase all most of the things!
21
Q

Why do multiple JSX tags need to be wrapped?

A

JSX looks like HTML, but under the hood it is transformed into plain JavaScript objects. You can’t return two objects from a function without wrapping them into an array. This explains why you also can’t return two JSX tags without wrapping them into another tag or a fragment.

22
Q

Extracting state logic into a reducer

A

Components with many state updates spread across many event handlers can get overwhelming.

For these cases, you can consolidate all the state update logic outside your component in a single function, called “reducer.”

Your event handlers become concise because they only specify the user “actions.”

The reducer function specifies how the state should update in response to each action

23
Q

Passing data deeply with context

A

Usually, you will pass information from a parent component to a child component via props.

But passing props can become inconvenient if you need to pass some prop through many components, or if many components need the same information.

Context lets the parent component make some information available to any component in the tree below it—no matter how deep it is—without passing it explicitly through props.

24
Q

Using reducer and context together

A

Reducers let you consolidate a component’s state update logic.

Context lets you pass information deep down to other components.

You can combine reducers and context together to manage state of a complex screen.

With this approach, a parent component with complex state manages it with a reducer.

Other components anywhere deep in the tree can read its state via context. They can also dispatch actions to update that state.