React Flashcards

Interview questions

1
Q

What is React?

A

open-source frontend JavaScript library which 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

Major features of React?

A
  • uses VirtualDOM instead 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

JSX is a XML-like syntax extension to ECMAScript. It provides syntactic sugar for the React.createElement() function, giving us expressiveness of JavaScript along with HTML like template syntax.

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

What is state in React?

A

State of a component is an object that holds some information that may change over the lifetime of the component. We should always try to make our state as simple as possible and minimize the number of stateful components.
State is used for internal communication inside a Component…

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

What are props in React?

A

Props are inputs to components. They are single values or objects containing a set of values that are passed to components on creation using a naming convention similar to HTML-tag attributes. They are data passed down from a parent component to a child component.

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

What is the difference between state and props?

A

Both props and state are plain JavaScript objects. While both of them hold information that influences the output of render, they are different in their functionality with respect to component. Props get passed to the component similar to function parameters whereas state is managed within the component similar to variables declared within a function.

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

Why should we not update the state directly?

A

If you try to update 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
8
Q

What happens when you call setState?

A

The first thing React will do when setState is called is merge the object you passed into setState into the current state of the component. This will kick off a process called reconciliation. The end goal of reconciliation is to, in the most efficient way possible, update the UI based on this new state. To do this, React will construct a new tree of React elements (which you can think of as an object representation of your UI). Once it has this tree, in order to figure out how the UI should change in response to the new state, React will diff this new tree against the previous element tree. By doing this, React will then know the exact changes which occurred, and by knowing exactly what changes occurred, will able to minimize its footprint on the UI by only making updates where absolutely necessary.

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

What’s the difference between an Element and a Component in React?

A
Simply put, a React element describes what you want to see on the screen. Not so simply put, a React element is an object representation of some UI. Elements can contain other Elements in their props. Creating a React element is cheap. Once an element is created, it is never mutated.
Whereas a component can be declared in several different ways. It can be a class with a render() method. Alternatively, in simple cases, it can be defined as a function. In either case, it takes props as an input, and returns an JSX tree as the output
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What are refs in React and why are they important?

A

The ref is used to return a reference to the element. They should be avoided in most cases, however, they can be useful when you need a direct access to the DOM element or an instance of a component.
In order to use them you add a ref attribute to your component whose value is a callback function which will receive the underlying DOM element or the mounted instance of the component as its first argument.

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

What are “key” props and what is the benefit of using them in arrays of elements?

A

Keys help React identify which items have changed, are added, or are removed from a list.
It’s important that each key be unique among siblings. We’ve talked a few times already about reconciliation and part of this reconciliation process is performing a diff of a new element tree with the most previous one. Keys make this process more efficient when dealing with lists because React can use the key on a child element to quickly know if an element is new or if it was just moved when comparing trees.

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

What is Virtual DOM?

A

The Virtual DOM (VDOM) is an in-memory representation of Real DOM. The representation of a UI is kept in memory and synced with the “real” DOM. It’s a step that happens between the render function being called and the displaying of elements on the screen. This entire process is called reconciliation.

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

How Virtual DOM works?

A
  1. Whenever any underlying data changes, the entire UI is re-rendered in Virtual DOM representation.
  2. Then the difference between the previous DOM representation and the new one is calculated.
  3. Once the calculations are done, the real DOM will be updated with only the things that have actually changed.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What is the difference between a controlled component and an uncontrolled component?

A

A controlled component is a component where React is in control and is the single source of truth for the form data. It lives in a component state.
An uncontrolled component is where your form data is handled by the DOM, instead of inside your React component.
You use refs to accomplish this.
–it’s recommend to use controlled components to implement forms.

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

What is Lifting State Up in React?

A

When several components need to share the same changing data then it is recommended to lift the shared state up to their closest common ancestor. That means if two child components share the same data from its parent, then move the state to parent instead of maintaining local state in both of the child components.

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