React Flashcards

1
Q

What is React?

A

React is a declarative, efficient, flexible open source front-end JavaScript library developed by Facebook in 2011. It follows the component-based approach for building reusable UI components, especially for single page application. It is used for developing interactive view layer of web and mobile apps. It was created by Jordan Walke, a software engineer at Facebook. It was initially deployed on Facebook’s News Feed section in 2011 and later used in its products like WhatsApp & Instagram.

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

What are the features of React?

A

React framework gaining quick popularity as the best framework among web developers. The main features of React are:

JSX
Components
One-way Data Binding
Virtual DOM
Simplicity
Performance
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What are the most crucial advantages of using React?

A

React is easy to learn and use

React comes with good availability of documentation, tutorials, and training resources. It is easy for any developer to switch from JavaScript background to React and easily understand and start creating web apps using React. Anyone with little knowledge of JavaScript can start building web applications using React.

React follows the MVC architecture.

React is the V (view part) in the MVC (Model-View-Controller) architecture model and is referred to as “one of the JavaScript frameworks.” It is not fully featured but has many advantages of the open-source JavaScript User Interface (UI) library, which helps execute the task in a better manner.

React uses Virtual DOM to improve efficiency.

React uses virtual DOM to render the view. The virtual DOM is a virtual representation of the real DOM. Each time the data changes in a react app, a new virtual DOM gets created. Creating a virtual DOM is much faster than rendering the UI inside the browser. Therefore, with the use of virtual DOM, the efficiency of the app improves. That’s why React provides great efficiency.

Creating dynamic web applications is easy.

In React, creating a dynamic web application is much easier. It requires less coding and gives more functionality. It uses JSX (JavaScript Extension), which is a particular syntax letting HTML quotes and HTML tag syntax to render particular subcomponents.

React is SEO-friendly.

React facilitates a developer to develop an engaging user interface that can be easily navigated in various search engines. It also allows server-side rendering, which is also helpful to boost the SEO of your app.

React allows reusable components.

React web applications are made up of multiple components where each component has its logic and controls. These components provide a small, reusable piece of HTML code as an output that can be reused wherever you need them. The code reusability helps developers to make their apps easier to develop and maintain. It also makes the nesting of the components easy and allows developers to build complex applications of simple building blocks. The reuse of components also increases the pace of development.

Support of handy tools

React provides a lot of handy tools that can make the task of the developers understandable and easier. Use these tools in Chrome and Firefox dev extension, allowing us to inspect the React component hierarchies in the virtual DOM. It also allows us to select the particular components and examine and edit their current props and state.

React has a rich set of libraries.

React has a huge ecosystem of libraries and provides you the freedom to choose the tools, libraries, and architecture for developing the best application based on your requirement.

Scope for testing the codes

React web applications are easy to

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

What are the biggest limitations of React?

A

React is just a library. It is not a complete framework.
It has a huge library which takes time to understand.
It may be difficult for the new programmers to understand and code.
React uses inline templating and JSX, which may be difficult and act as a barrier. It also makes the coding complex.

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

What is JSX?

A

JSX stands for JavaScript XML. It is a React extension which allows writing JavaScript code that looks similar to HTML. It makes HTML file easy to understand. The JSX file makes the React application robust and boosts its performance. JSX provides you to write XML-like syntax in the same file where you write JavaScript code, and then preprocessor (i.e., transpilers like Babel) transform these expressions into actual JavaScript code. Just like XML/HTML, JSX tags have a tag name, attributes, and children.

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

Why can’t browsers read JSX?

A

Browsers cannot read JSX directly because they can only understand JavaScript objects, and JSX is not a regular JavaScript object. Thus, we need to transform the JSX file into a JavaScript object using transpilers like Babel and then pass it to the browser.

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

Why we use JSX?

A

It is faster than regular JavaScript because it performs optimization while translating the code to JavaScript.
Instead of separating technologies by putting markup and logic in separate files, React uses components that contain both.
t is type-safe, and most of the errors can be found at compilation time.
It makes easier to create templates.

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

What do you understand by Virtual DOM?

A

A Virtual DOM is a lightweight JavaScript object which is an in-memory representation of real DOM. It is an intermediary step between the render function being called and the displaying of elements on the screen. It is similar to a node tree which lists the elements, their attributes, and content as objects and their properties. The render function creates a node tree of the React components and then updates this node tree in response to the mutations in the data model caused by various actions done by the user or by the system.

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

Explain the working of Virtual DOM.

A

Virtual DOM works in three steps:

  1. Whenever any data changes in the React App, the entire UI is re-rendered in Virtual DOM representation.

React Interview Questions1
2. Now, the difference between the previous DOM representation and the new DOM is calculated.

React Interview Questions2
3. Once the calculations are completed, the real DOM updated with only those things which are changed.

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

How is React different from Angular?

A
Angular
Release: 2010
Language: JavaScript, HTML
Type: Open Source MVC Framework
Rendering: Client-Side
Data-Binding: Bi-directional
DOM: Regular DOM
Testing: Unit and Integration Testing
App Architecture: MVC
Performance: Slow
React
Release: 2013
Language: JSX
Type: Open Source JS Framework
Rendering: Server-Side
Data-Binding: Uni-Directional
DOM: Virtual DOM
Testing: Unit Testing
App Architecture: Flux
Performance: Fast due to virtual DOM
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

How React’s ES6 syntax is different from ES5 syntax?

A

The React’s ES6 syntax has changed from ES5 syntax in the following aspects.

require vs. Import
exports vs. export
component and function
props
state
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What is React Native?

A

Initial release in 2015.
It is used for developing mobile applications.
It is not platform independent. It takes more effort to be executed on all platforms.
It comes with built-in animation libraries.
It has built-in Navigator library for navigating mobile applications.
It does not use HTML tags.
In this, Native uses its API to render code for mobile applications.

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

What is the difference between Real DOM and Virtual DOM?

A

Real DOM
Updates slower, can directly update HTML.
The virtual DOM updates the JSX if the element updates.
Real DOM manipulation is very expensive.
A lot of memory wastage in the real DOM.
Virtual DOM
Update faster, cannot directly update HTML.
Virtual DOM is manipulated easily.
No memory wastage in the virtual DOM.

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

What do you understand from “In React, everything is a component.”

A

In React, components are the building blocks of React applications. These components divide the entire React application’s UI into small, independent, and reusable pieces of code. React renders each of these components independently without affecting the rest of the application UI. Hence, we can say that, in React, everything is a component.

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

Explain the purpose of render() in React.

A

It is mandatory for each React component to have a render() function. Render function is used to return the HTML which you want to display in a component. If you need to rendered more than one HTML element, you need to grouped together inside single enclosing tag (parent tag) such as <div>, , etc. This function returns the same result each time it is invoked.

</div>

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

What is Props?

A

Props stand for “Properties” in React. They are read-only inputs to components. Props are an object which stores the value of attributes of a tag and work similar to the HTML attributes. It gives a way to pass data from the parent to the child components throughout the application.

It is similar to function arguments and passed to the component in the same way as arguments passed in a function.

Props are immutable so we cannot modify the props from inside the component. Inside the components, we can add attributes called props. These attributes are available in the component as this.props and can be used to render dynamic data in our render method.

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

What is a State in React?

A

The State is an updatable structure which holds the data and information about the component. It may be changed over the lifetime of the component in response to user action or system event. It is the heart of the react component which determines the behavior of the component and how it will render. It must be kept as simple as possible.

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

Differentiate between States and Props.

A

Props
Are read-only.
Are immutable.
Props allow you to pass data from one component to other components as an argument.
Props can be accessed by the child component, and are used to communicate between components.
Stateless components can have props.
Props make components reusable.
Props are external and controlled by whatever renders the components.

State
State changes can be asynchronous.
State is mutable.
State holds information about the components.
State cannot be accessed by child components.
States can be used for rendering dynamic changes with the component.
Stateless components cannot have state.
The State cannot make components reusable.
State is internal and controlled by the component itself.

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

How can you update the State of a component?

A

We can update the State of a component using this.setState() method. This method does not always replace the State immediately. Instead, it only adds changes to the original State. It is a primary method which is used to update the user interface(UI) in response to event handlers and server responses.

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

Differentiate between stateless and stateful components.

A

Stateless Component
The stateless components do not hold or manage state.
It does not contain the knowledge of past, current, and possible future state changes.
It is also known as a functional component.
It is simple and easy to understand.
It does not work with any lifecycle method of React.
The stateless components cannot be reused.

Stateful Component
It can contain the knowledge of past, current, and possible future changes in state.
It is also known as a class component.
It is complex as compared to the stateless component.
It can work with all lifecycle method of React.
The stateful components can be reused.

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

What is arrow function in React? How is it used?

A

The Arrow function is the new feature of the ES6 standard. If you need to use arrow functions, it is not necessary to bind any event to ‘this.’ Here, the scope of ‘this’ is global and not limited to any calling function. So If you are using Arrow Function, there is no need to bind ‘this’ inside the constructor. It is also called ‘fat arrow ‘(=>) functions.

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

What is an event in React?

A

An event is an action which triggers as a result of the user action or system generated event like a mouse click, loading of a web page, pressing a key, window resizes, etc. In React, the event handling system is very similar to handling events in DOM elements. The React event handling system is known as Synthetic Event, which is a cross-browser wrapper of the browser’s native event.

Handling events with React have some syntactical differences, which are:

React events are named as camelCase instead of lowercase.
With JSX, a function is passed as the event handler instead of a string.

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

How do you create an event in React?

A

<div>Click Me</div>

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

What are synthetic events in React?

A

A synthetic event is an object which acts as a cross-browser wrapper around the browser’s native event. It combines the behavior of different browser’s native event into one API, including stopPropagation() and preventDefault().

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

What is the difference between controlled and uncontrolled components?

A

Controlled
It does not maintain its internal state.
Here, data is controlled by the parent component.
It accepts its current value as a prop.
It allows validation control.
It has better control over the form elements and data.

Uncontrolled
It maintains its internal states.
Here, data is controlled by the DOM itself.
It uses a ref for their current values.
It does not allow validation control.
It has limited control over the form elements and data.

26
Q

Explain the Lists in React.

A

Lists are used to display data in an ordered format. In React, Lists can be created in a similar way as we create it in JavaScript. We can traverse the elements of the list using the map() function.

27
Q

What is the significance of keys in React?

A

A key is a unique identifier. In React, it is used to identify which items have changed, updated, or deleted from the Lists. It is useful when we dynamically created components or when the users alter the lists. It also helps to determine which components in a collection needs to be re-rendered instead of re-rendering the entire set of components every time. It increases application performance.

28
Q

How are forms created in React?

A

Forms allow the users to interact with the application as well as gather information from the users. Forms can perform many tasks such as user authentication, adding user, searching, filtering, etc. A form can contain text fields, buttons, checkbox, radio button, etc.

React offers a stateful, reactive approach to build a form. The forms in React are similar to HTML forms. But in React, the state property of the component is only updated via setState(), and a JavaScript function handles their submission. This function has full access to the data which is entered by the user into a form.

29
Q

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

A

The different phases of React component’s lifecycle are:

Initial Phase: It is the birth phase of the React lifecycle when the component starts its journey on a way to the DOM. In this phase, a component contains the default Props and initial State. These default properties are done in the constructor of a component.

Mounting Phase: In this phase, the instance of a component is created and added into the DOM.

Updating Phase: It is the next phase of the React lifecycle. In this phase, we get new Props and change State. This phase can potentially update and re-render only when a prop or state change occurs. The main aim of this phase is to ensure that the component is displaying the latest version of itself. This phase repeats again and again.

Unmounting Phase: It is the final phase of the React lifecycle, where the component instance is destroyed and unmounted(removed) from the DOM.

30
Q

Explain the lifecycle methods of React components in detail.

A

The important React lifecycle methods are:

getInitialState(): It is used to specify the default value of this.state. It is executed before the creation of the component.
componentWillMount(): It is executed before a component gets rendered into the DOM.
componentDidMount(): It is executed when the component gets rendered and placed on the DOM. Now, you can do any DOM querying operations.
componentWillReceiveProps(): It is invoked when a component receives new props from the parent class and before another render is called. If you want to update the State in response to prop changes, you should compare this.props and nextProps to perform State transition by using this.setState() method.
shouldComponentUpdate(): It is invoked when a component decides any changes/updation to the DOM and returns true or false value based on certain conditions. If this method returns true, the component will update. Otherwise, the component will skip the updating.
componentWillUpdate(): It is invoked before rendering takes place in the DOM. Here, you can't change the component State by invoking this.setState() method. It will not be called, if shouldComponentUpdate() returns false.
componentDidUpdate(): It is invoked immediately after rendering takes place. In this method, you can put any code inside this which you want to execute once the updating occurs.
componentWillUnmount(): It is invoked immediately before a component is destroyed and unmounted permanently. It is used to clear up the memory spaces such as invalidating timers, event listener, canceling network requests, or cleaning up DOM elements. If a component instance is unmounted, you cannot mount it again.
31
Q

What are Pure Components?

A

Pure components introduced in React 15.3 version. The React.Component and React.PureComponent differ in the shouldComponentUpdate() React lifecycle method. This method decides the re-rendering of the component by returning a boolean value (true or false). In React.Component, shouldComponentUpdate() method returns true by default. But in React.PureComponent, it compares the changes in state or props to re-render the component. The pure component enhances the simplicity of the code and performance of the application.

32
Q

What are Higher Order Components(HOC)?

A

In React, Higher Order Component is an advanced technique for reusing component logic. It is a function that takes a component and returns a new component. In other words, it is a function which accepts another function as an argument. According to the official website, it is not the feature(part) in React API, but a pattern that emerges from React’s compositional nature.

33
Q

What can you do with Higher Order Components (HOC)?

A

You can do many tasks with HOC, some of them are given below:

Code Reusability
Props manipulation
State manipulation
Render highjacking

34
Q

What is the difference between Element and Component?

A
Element
An element is a plain JavaScript object which describes the component state and DOM node, and its desired properties.
It only holds information about the component type, its properties, and any child elements inside it.
It is immutable.
We cannot apply any methods on elements.
Example:
const element = React.createElement(
'div', {id: 'login-btn'}, 'Login' )

Component
A component is the core building block of React application. It is a class or function which accepts an input and returns a React element.
It can contain state and props and has access to the React lifecycle methods.
It is mutable.
We can apply methods on components.
Example:
function Button ({ onLogin }) {
return React.createElement(
‘div’, {id: ‘login-btn’, onClick: onLogin},
‘Login’ )}

35
Q

How to write comments in React?

A
{/* Single Line comment */}  
{ /*  
   Multi 
   line 
   comment 
*/ }
36
Q

Why is it necessary to start component names with a capital letter?

A

In React, it is necessary to start component names with a capital letter. If we start the component name with lower case, it will throw an error as an unrecognized tag. It is because, in JSX, lower case tag names are considered as HTML tags.

37
Q

What are fragments?

A

In was introduced in React 16.2 version. In React, Fragments are used for components to return multiple elements. It allows you to group a list of multiple children without adding an extra node to the DOM.

38
Q

Why are fragments better than container divs?

A

Fragments are faster and consume less memory because it did not create an extra DOM node.
Some CSS styling like CSS Grid and Flexbox have a special parent-child relationship and add <div> tags in the middle, which makes it hard to keep the desired layout.
The DOM Inspector is less cluttered.</div>

39
Q

How to apply validation on props in React?

A

Props validation is a tool which helps the developers to avoid future bugs and problems. It makes your code more readable. React components used special property PropTypes that help you to catch bugs by validating data types of values passed through props, although it is not necessary to define components with propTypes.

We can apply validation on props using App.propTypes in React component. When some of the props are passed with an invalid type, you will get the warnings on JavaScript console. After specifying the validation patterns, you need to set the App.defaultProps.

class App extends React.Component {  
          render() {}  
}  
Component.propTypes = { /*Definition */};
40
Q

What is create-react-app?

A

Create React App is a tool introduced by Facebook to build React applications. It provides you to create single-page React applications. The create-react-app are preconfigured, which saves you from time-consuming setup and configuration like Webpack or Babel. You need to run a single command to start the React project, which is given below.

$ npx create-react-app my-app

41
Q

How can you create a component in React?

A
There are two possible ways to create a component in React:
Function Components: This is the simplest way to create a component in React. These are the pure JavaScript functions that accept props object as the first parameter and return React elements:
function Greeting({ message }) {  
  return <h1>{`Hello, ${message}`}</h1>  }
Class Components: The class components method facilitates you to use ES6 class to define a component. The above function component can be written as:
class Greeting extends React.Component {  
  render() {  
    return <h1>{`Hello, ${this.props.message}`}</h1>    }  }
42
Q

When do we prefer to use a class component over a function component?

A

If a component needs state or lifecycle methods, we should use the class component; otherwise, use the function component. However, after React 16.8, with the addition of Hooks, you could use state, lifecycle methods, and other features that were only available in the class component right in your function component.

43
Q

Is it possible for a web browser to read JSX directly?

A

Web browsers can’t read JSX directly. This is because the web browsers are built to read the regular JS objects only, and JSX is not a regular JavaScript object.

If you want a web browser to read a JSX file, you must transform the files into a regular JavaScript object. For this purpose, Babel is used.

44
Q

What do you understand by the state in React?

A

In react, the state of a component is an object that holds some information that may change over the component’s lifetime. It would be best to try to make your state as simple as possible and minimize the number of stateful components.
The state is very similar to props, but it is private and fully controlled by the component. i.e., It is not accessible to any other component till the owner component decides to pass it.

45
Q

What do you understand by props in React?

A

In React, the props are inputs to components. They are single values or objects containing a set of values 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.

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

Pass custom data to your component.
Trigger state changes.
Use via this.props.reactProp inside component’s render() method.

46
Q

What do you understand by refs in React?

A

Refs is the shorthand used for references in React. It is an attribute which helps to store a reference to particular DOM nodes or React elements. It provides a way to access React DOM nodes or React elements and how to interact with it. It is used when we want to change the value of a child component, without making the use of props.

47
Q

How to create refs?

A

Refs can be created by using React.createRef() and attached to React elements via the ref attribute. It is commonly assigned to an instance property when a component is created, and then can be referenced throughout the component.

48
Q

What is the use of Refs?

A

The Ref in React is used in the following cases:

It is used to return a reference to the element.
It is used when we need DOM measurements such as managing focus, text selection, or media playback.
It is used in triggering imperative animations.
It is used when integrating with third-party DOM libraries.
It can also use as in callbacks.

49
Q

What is React Router?

A

React Router is a standard routing library system built on top of the React. It is used to create Routing in the React application using React Router Package. It helps you to define multiple routes in the app. It provides the synchronous URL on the browser with data that will be displayed on the web page. It maintains the standard structure and behavior of the application and mainly used for developing single page web applications.

50
Q

Why do we need a Router in React?

A

React Router plays an important role to display multiple views in a single page application. It is used to define multiple routes in the app. When a user types a specific URL into the browser, and if this URL path matches any ‘route’ inside the router file, the user will be redirected to that particular Route. So, we need to add a Router library to the React app, which allows creating multiple routes with each leading to us a unique view.

  <h1>React Router Example</h1>
51
Q

List down the advantages of React Router.

A

The important advantages of React Router are given below:
In this, it is not necessary to set the browser history manually.
Link uses to navigate the internal links in the application. It is similar to the anchor tag.
It uses Switch feature for rendering.
The Router needs only a Single Child element.
In this, every component is specified in .
The packages are split into three packages, which are Web, Native, and Core. It supports the compact size of the React application.

52
Q

How is React Router different from Conventional Routing?

A

Conventional Routing
In Conventional Routing, each view contains a new file.
The HTTP request is sent to a server to receive the corresponding HTML page.
In this, the user navigates across different pages for each view.
React Routing
In React Routing, there is only a single HTML page involved.
Only the History attribute is changed.
In this, the user is thinking he is navigating across different pages, but its an illusion only.

53
Q

What is the “Router may have only one child element” warning?

A

It is because you have not wrapped your Route’s in a block or <div> block which renders a route exclusively.</div>

54
Q

Why switch keyword used in React Router v4?

A

The ‘switch’ keyword is used to display only a single Route to rendered amongst the several defined Routes. The component is used to render components only when the path will be matched. Otherwise, it returns to the not found component.

55
Q

How to use styles in React?

A

We can use style attribute for styling in React applications, which adds dynamically-computed styles at render time. It accepts a JavaScript object in camelCased properties rather than a CSS string. The style attribute is consistent with accessing the properties on DOM nodes in JavaScript.

56
Q

How many ways can we style the React Component?

A

We can style React Component in mainly four ways, which are given below:

Inline Styling
CSS Stylesheet
CSS Module
Styled Components

57
Q

Explain CSS Module styling in React.

A

CSS Module is a CSS file where all class names and animation names are scoped locally by default. It is available only for the component which imports it, and without your permission, it cannot be applied to any other Components. You can create CSS Module file with the .module.css extension.

58
Q

What are Styled Components?

A

Styled-Components is a library for React. It is the successor of CSS Modules. It uses enhance CSS for styling React component systems in your application, which is written with a mixture of JavaScript and CSS. It is scoped to a single component and cannot leak to any other element in the page.

The styled-components provides:

Automatic critical CSS
No class name bugs
Easier deletion of CSS
Simple dynamic styling
Painless maintenance
59
Q

What are hooks in React?

A

Hooks are the new feature introduced in React 16.8 version that facilitates us to use state and other React features without writing a class.

60
Q

What are the rules you should follow for the hooks in React?

A

You should call hooks only at the top level of your React functions and not inside the loops, conditions, or nested functions. This is used to ensure that hooks are called in the same order each time a component renders, and it also preserves the state of hooks between multiple useState and useEffect calls.
You should call hooks from React functions only. Don’t call hooks from regular JavaScript functions.