If you’re looking for ReactJS Interview Questions and Answers for Experienced & Freshers, you are at right place.ReactJs is undoubtedly an amazing open source JavaScript front end UI library for creating interactive, stateful & reusable UI components for web as well as mobile applications. In this ReactJs Interview Questions and Answers Tutorial, we have tried to cover almost all main topics related to React.

If there is any new ReactJS interview question that have been asked to you, kindly post it in the the comment section.


 

1. What is React?

Ans:

React is a front end JavaScript library developed by Facebook in 2011. It follows the component based approach which helps in building reusable UI components. It is used for developing complex and interactive web and mobile UI. Even though, it was open-sourced only in 2015, it has one of the largest communities supporting it.

2. What are the features of React?

Ans:

Major features of React are listed below:

  • It uses the virtual DOM instead of the real DOM.
  • It uses server-side rendering.
  • It follows uni-directional data flow or data binding.

3. List some of the major advantages of React.

Ans:

Some of the major advantages of React are:

  • It increases the application’s performance
  • It can be conveniently used on the client as well as server side
  • Because of JSX, code’s readability increases
  • React is easy to integrate with other frameworks like Meteor, Angular, etc
  • Using React, writing UI test cases become extremely easy

4. What are the limitations of React?

Ans:

Limitations of React are listed below:

  • React is just a library, not a full-blown framework
  • Its library is very large and takes time to understand
  • It can be little difficult for the novice programmers to understand
  • Coding gets complex as it uses inline templating and JSX

5. What is JSX?

Ans:

JSX is a shorthand for JavaScript XML. This is a type of file used by React which utilizes the expressiveness of JavaScript along with HTML like template syntax. This makes the HTML file really easy to understand. This file makes applications robust and boosts its performance. Below is an example of JSX:

render(){
return(
<div>
<h1> Hello World from Edureka!!</h1>
</div>
);
}

6. What do you understand by Virtual DOM? Explain its working.

Ans:

A virtual DOM is a lightweight JavaScript object which originally is just the copy of the real DOM. It is a node tree that lists the elements, their attributes and content as Objects and their properties. React’s render function creates a node tree out of the React components. It then updates this tree in response to the mutations in the data model which is caused by various actions done by the user or by the system.
This Virtual DOM works in three simple steps.
Whenever any underlying data changes, the entire UI is re-rendered in Virtual DOM representation.

Virtual DOM

Then the difference between the previous DOM representation and the new one is calculated.

DOM

 

Once the calculations are done, the real DOM will be updated with only the things that have actually changed.

Real DOM (updated)

7. Differentiate between Real DOM and Virtual DOM.

Ans:

 

Real DOM

Virtual  DOM

1. It updates slow.

1. It updates faster.

2. Can directly update HTML.

2. Can’t directly update HTML.

3. Creates a new DOM if element updates.

3. Updates the JSX if element updates.

4. DOM manipulation is very expensive.

4. DOM manipulation is very easy.

5. Too much of memory wastage.

5. No memory wastage.

8. Why can’t browsers read JSX?

Ans:

Browsers can only read JavaScript objects but JSX in not a regular JavaScript object. Thus to enable a browser to read JSX, first, we need to transform JSX file into a JavaScript object using JSX transformers like Babel and then pass it to the browser.

9. How different is React’s ES6 syntax when compared to ES5?

Ans:

Syntax has changed from ES5 to ES6 in following aspects:
require vs import

// ES5
var React = require(‘react’);

// ES6
import React from ‘react’;
export vs exports

// ES5
module.exports = Component;

// ES6
export default Component;
component and function

// ES5
var MyComponent = React.createClass({
render: function() {
return <h3>Hello Edureka!</h3>;
}
});

// ES6
class MyComponent extends React.Component {
render() {
return <h3>Hello Edureka!</h3>;
}
}
props

// ES5
var App = React.createClass({
propTypes: { name: React.PropTypes.string },
render: function() {
return <h3>Hello, {this.props.name}!</h3>;
}
});

// ES6
class App extends React.Component {
render() {
return <h3>Hello, {this.props.name}!</h3>;
}
}
state

// ES5
var App = React.createClass({
getInitialState: function() {
return { name: ‘world’ };
},
render: function() {
return <h3>Hello, {this.state.name}!</h3>;
}
});

// ES6
class App extends React.Component {
constructor() {
super();
this.state = { name: ‘world’ };
}
render() {
return <h3>Hello, {this.state.name}!</h3>;
}
}

10. How is React different from Angular?

Ans:

 

TOPIC

REACT

ANGULAR

1. ARCHITECHTURE

Only the View of MVC

Complete MVC

2. RENDERING

Server side rendering

Client side rendering

3. DOM

Uses virtual DOM

Uses real DOM

4. DATA BINDING

One-way data binding

Two-way data binding

5. DEBUGGING

Compile time debugging

Run time debugging

6. AUTHOR

Facebook

Google



 

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

Ans:

Components are the building blocks of a React application’s UI. These components split up the entire UI into small independent and reusable pieces. Then it renders each of these components independent of each other without affecting the rest of the UI.

12. Explain the purpose of render() in React.

Ans:

Each React component must have a render() mandatorily. It returns a single React element which is the representation of the native DOM component. If more than one HTML element needs to be rendered, then they must be grouped together inside one enclosing tag such as <form>, <group>,<div> etc. This function must be kept pure i.e., it must return the same result each time it is invoked.

13. How can you embed two or more components into one?

Ans:

We can embed components into one in the following way:

class MyComponent extends React.Component{
render(){
return(
<div>
<h1>Hello</h1>
<Header/>
</div>
);
}
}
class Header extends React.Component{
render(){
return <h1>Header Component</h1>
};
}
ReactDOM.render(
<MyComponent/>, document.getElementById('content')
);

14. What is Props?

Ans:

Props are short hand for Properties in React. They are read-only components which must be kept pure i.e. immutable. They are always passed down from the parent to the child components through out the application. A child component can never send a prop back to the parent component. This help in maintaining the unidirectional data flow and are generally used to render the dynamically generated data.

15. What is a state in React and how is it used?

Ans:

States are the heart of React components. States are the source of data and must be kept as simple as possible. Basically, states are the objects which determine components rendering and behavior. They are mutable unlike the props and create dynamic and interactive components. They are accessed via this.state().

16. Differentiate between states and props.

Ans:

Conditions

State

Props

1. Receive initial value from parent component

Yes

Yes

2. Parent component can change value

No

Yes

3. Set default values inside component

Yes

Yes

4. Changes inside component

Yes

No

5. Set initial value for child components

Yes

Yes

6. Changes inside child components

No

Yes

17. How can you update the state of a component?

Ans:

State of a component can be updated using this.setState().

class MyComponent extends React.Component {
constructor() {
super();
this.state = {
name: 'Maxx',
id: '101'
}
}
render()
{
setTimeout(()=>{this.setState({name:'Jaeha', id:'222'})},2000)
return (
<div>
<h1>Hello {this.state.name}</h1>
<h2>Your Id is {this.state.id}</h2>
</div>
);
}
}
ReactDOM.render(
<MyComponent/>, document.getElementById('content')
);

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

Ans:

Arrow functions are more of brief syntax for writing the function expression. They are also called ‘fat arrow‘ (=>) the functions. These functions allow to bind the context of the components properly since in ES6 auto binding is not available by default. Arrow functions are mostly useful while working with the higher order functions.

//General way
render() {
return(
<MyInput onChange={this.handleChange.bind(this) } />
);
}
//With Arrow Function
render() {
return(
<MyInput onChange={ (e) => this.handleOnChange(e) } />
);
}

19. Differentiate between stateful and stateless components.

Ans:

Stateful Component

Stateless Component

1. Stores info about component’s state change in memory

1. Calculates the internal state of the components

2. Have authority to change state

2. Do not have the authority to change state

3. Contains the knowledge of past, current and possible future changes in state

3. Contains no knowledge of past, current and possible future state changes

4. Stateless components notify them about the requirement of the state change, then they send down the props to them.

4. They receive the props from the Stateful components and treat them as callback functions.

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

Ans:

There are three different phases of React component’s lifecycle:

  • Initial Rendering Phase: This is the phase when the component is about to start its life journey and make its way to the DOM.
  • Updating Phase: Once the component gets added to the DOM, it can potentially update and re-render only when a prop or state change occurs. That happens only in this phase.
  • Unmounting Phase: This is the final phase of a component’s life cycle in which the component is destroyed and removed from the DOM.



 

21. Explain the lifecycle methods of React components in detail.

Ans:

Some of the most important lifecycle methods are:
componentWillMount() – Executed just before rendering takes place both on the client as well as server-side.
componentDidMount() – Executed on the client side only after the first render.
componentWillReceiveProps() – Invoked as soon as the props are received from the parent class and before another render is called.
shouldComponentUpdate() – Returns true or false value based on certain conditions. If you want your component to update, return true else return false. By default, it returns false.
componentWillUpdate() – Called just before rendering takes place in the DOM.
componentDidUpdate() – Called immediately after rendering takes place.
componentWillUnmount() – Called after the component is unmounted from the DOM. It is used to clear up the memory spaces.

22. What is an event in React?

Ans:

In React, events are the triggered reactions to specific actions like mouse hover, mouse click, key press, etc. Handling these events are similar to handling events in DOM elements. But there are some syntactical differences like:

  • Events are named using camel case instead of just using the lowercase.
  • Events are passed as functions instead of strings.

The event argument contains a set of properties, which are specific to an event. Each event type contains its own properties and behavior which can be accessed via its event handler only.

23. How do you create an event in React?

Ans:

class Display extends React.Component({ 
show(evt) {
// code
},
render() {
// Render the div with an onClick prop (value is a function)
return (
<div onClick={this.show}>Click Me!</div>
);
}
});

24. What are synthetic events in React?

Ans:

Synthetic events are the objects which act as a cross-browser wrapper around the browser’s native event. They combine the behavior of different browsers into one API. This is done to make sure that the events show consistent properties across different browsers.

25. What do you understand by refs in React?

Ans:

Refs is the short hand for References in React. It is an attribute which helps to store a reference to a particular React element or component, which will be returned by the components render configuration function. It is used to return references to a particular element or component returned by render(). They come in handy when we need DOM measurements or to add methods to the components.

class ReferenceDemo extends React.Component{
display() {
const name = this.inputDemo.value;
document.getElementById('disp').innerHTML = name;
}
render() {
return(
<div>
Name: <input type="text" ref={input => this.inputDemo = input} />
<button name="Click" onClick={this.display}>Click</button>
<h2>Hello <span id="disp"></span> !!!</h2>
</div>
);
}
}

26. List some of the cases when you should use Refs.

Ans:

Following are the cases when refs should be used:

  • When you need to manage focus, select text or media playback
  • To trigger imperative animations
  • Integrate with third-party DOM libraries

27. How do you modularize code in React?

Ans:

We can modularize code by using the export and import properties. They help in writing the components separately in different files.

//ChildComponent.jsx
export default class ChildComponent extends React.Component {
render() {
return(
<div>
<h1>This is a child component</h1>
</div>
);
}
}

//ParentComponent.jsx
import ChildComponent from './childcomponent.js';
class ParentComponent extends React.Component {
render() {
return(
<div>
<App />
</div>
);
}
}

28. How are forms created in React?

Ans:

React forms are similar to HTML forms. But in React, the state is contained in the state property of the component and is only updated via setState(). Thus the elements can’t directly update their state and their submission is handled by a JavaScript function. This function has full access to the data that is entered by the user into a form.

handleSubmit(event) {
alert('A name was submitted: ' + this.state.value);
event.preventDefault();
}

render() {
return (
<form onSubmit={this.handleSubmit}>
<label>
Name:
<input type="text" value={this.state.value} onChange={this.handleSubmit} />
</label>
<input type="submit" value="Submit" />
</form>
);
}

29. What do you know about controlled and uncontrolled components?

Ans:

Controlled Components

Uncontrolled Components

1. They do not maintain their own state

1. They maintain their own state

2. Data is controlled by the parent component

2. Data is controlled by the DOM

3. They take in the current values through props and then notify the changes via callbacks

3. Refs are used to get their current values

30. What are Higher Order Components(HOC)?

Ans:

Higher Order Component is an advanced way of reusing the component logic. Basically, it’s a pattern that is derived from React’s compositional nature. HOC are custom components which wraps another component within it. They can accept any dynamically provided child component but they won’t modify or copy any behavior from their input components. You can say that HOC are ‘pure’ components.


 

31. What can you do with HOC?

Ans:

HOC can be used for many tasks like:

  • Code reuse, logic and bootstrap abstraction
  • Render High jacking
  • State abstraction and manipulation
  • Props manipulation

32. What are Pure Components?

Ans:

Pure components are the simplest and fastest components which can be written. They can replace any component which only has a render(). These components enhance the simplicity of the code and performance of the application.

33. What is the significance of keys in React?

Ans:

Keys are used for identifying unique Virtual DOM Elements with their corresponding data driving the UI. They help React to optimize the rendering by recycling all the existing elements in the DOM. These keys must be a unique number or string, using which React just reorders the elements instead of re-rendering them. This leads to increase in application’s performance.

34. What were the major problems with MVC framework?

Ans:

Following are some of the major problems with MVC framework:

  • DOM manipulation was very expensive
  • Applications were slow and inefficient
  • There was huge memory wastage
  • Because of circular dependencies, complicated model was created around models and views

35. Explain Flux.

Ans:

Flux is an architectural pattern which enforces the uni-directional data flow. It controls derived data and enables communication between multiple components using a central Store which has authority for all data. Any update in data throughout the application must occur here only. Flux provides stability to the application and reduces run-time errors

36. What is Redux?

Ans:

Redux is one of the hottest libraries for front end development in today’s marketplace. It is a predictable state container for JavaScript applications and is used for the entire applications state management. Applications developed with Redux are easy to test and can run in different environments showing consistent behavior.

37. What are the three principles that Redux follows?

Ans:

Single source of truth: The state of the entire application is stored in an object/ state tree within a single store. The single state tree makes it easier to keep track of changes over time and debug or inspect the application.
State is read-only: The only way to change the state is to trigger an action. An action is a plain JS object describing the change. Just like state is the minimal representation of data, the action is the minimal representation of the change to that data.
Changes are made with pure functions: In order to specify how the state tree is transformed by actions, you need pure functions. Pure functions are those whose return value depend solely on the values of their arguments.

38. What do you understand by “Single source of truth”?

Ans:

Redux uses ‘Store’ for storing the application’s entire state at one place. So all the component’s state are stored in the Store and they receive updates from the Store itself. The single state tree makes it easier to keep track of changes over time and debug or inspect the application.

39. List down the components of Redux.

Ans:

Redux is composed of the following components:

Action – It’s an object that describes what happened.
Reducer – It is a place to determine how the state will change.
Store – State/ Object tree of the entire application is saved in the Store.
View – Simply displays the data provided by the Store.

40. Show how the data flows through Redux?

Ans:

The Data Flows Through Redux

The Data Flows Through Redux



 

41. How are Actions defined in Redux?

Ans:

Actions in React must have a type property that indicates the type of ACTION being performed. They must be defined as a String constant and you can add more properties to it as well. In Redux, actions are created using the functions called Action Creators. Below is an example of Action and Action Creator:

function addTodo(text) {
return {
type: ADD_TODO,
text
}
}

42. Explain the role of Reducer.

Ans:

Reducers are pure functions which specify how the application’s state changes in response to an ACTION. Reducers work by taking in the previous state and action, and then it returns a new state. It determines what sort of update needs to be done based on the type of the action, and then returns new values. It returns the previous state as it is, if no work needs to be done.

43. What is the significance of Store in Redux?

Ans:

A store is a JavaScript object which can hold the application’s state and provide a few helper methods to access the state, dispatch actions and register listeners. The entire state/ object tree of an application is saved in a single store. As a result of this, Redux is very simple and predictable. We can pass middleware to the store to handle processing of data as well as to keep a log of various actions that change the state of stores. All the actions return a new state via reducers.

44. How is Redux different from Flux?

Ans:

Flux

Redux

1. The Store contains state and change logic

1. Store and change logic are separate

2. There are multiple stores

2. There is only one store

3. All the stores are disconnected and flat

3. Single store with hierarchical reducers

4. Has singleton dispatcher

4. No concept of dispatcher

5. React components subscribe to the store

5. Container components utilize connect

6. State is mutable

6. State is immutable

45. What are the advantages of Redux?

Ans:

Advantages of Redux are listed below:

  • Predictability of outcome – Since there is always one source of truth, i.e. the store, there is no confusion about how to sync the current state with actions and other parts of the application.
  • Maintainability – The code becomes easier to maintain with a predictable outcome and strict structure.
  • Server side rendering – You just need to pass the store created on the server, to the client side. This is very useful for initial render and provides a better user experience as it optimizes the application performance.
  • Developer tools – From actions to state changes, developers can track everything going on in the application in real time.
  • Community and ecosystem – Redux has a huge community behind it which makes it even more captivating to use. A large community of talented individuals contribute to the betterment of the library and develop various applications with it.
  • Ease of testing – Redux’s code is mostly functions which are small, pure and isolated. This makes the code testable and independent.
  • Organization – Redux is precise about how code should be organized, this makes the code more consistent and easier when a team works with it.

46. What is React Router?

Ans:

React Router is a powerful routing library built on top of React, which helps in adding new screens and flows to the application. This keeps the URL in sync with data that’s being displayed on the web page. It maintains a standardized structure and behavior and is used for developing single page web applications. React Router has a simple API.

47. Why is switch keyword used in React Router v4?

Ans:

Although a <div> is used to encapsulate multiple routes inside the Router. The ‘switch’ keyword is used when you want to display only a single route to be rendered amongst the several defined routes. The <switch> tag when in use matches the typed URL with the defined routes in sequential order. When the first match is found, it renders the specified route. Thereby bypassing the remaining routes.

48. Why do we need a Router in React?

Ans:

A Router is used to define multiple routes and when a user types a specific URL, if this URL matches the path of any ‘route’ defined inside the router, then the user is redirected to that particular route. So basically, we need to add a Router library to our app that allows creating multiple routes with each leading to us a unique view.

<switch>
<route exact path=’/’ component={Home}/>
<route path=’/posts/:id’ component={Newpost}/>
<route path=’/posts’ component={Post}/>
</switch>

49. List down the advantages of React Router.

Ans:

Few advantages are:

  • Just like how React is based on components, in React Router v4, the API is ‘All About Components’. A Router can be visualized as a single root component (<BrowserRouter>) in which we enclose the specific child routes (<route>).
  • No need to manually set History value: In React Router v4, all we need to do is wrap our routes within the<BrowserRouter> component.
  • The packages are split: Three packages one each for Web, Native and Core. This supports the compact size of our application. Its easy to switch over based on similar coding style.

50. How is React Router different from conventional routing?

Ans:

Topic

Conventional Routing

React Routing

PAGES INVOLVED

Each view corresponds to a new file

Only single HTML page is involved

URL CHANGES

A HTTP request is sent to server and corresponding HTML page is received

Only the History attribute is changed

FEEL

User actually navigates across different pages for each view

User is duped thinking he is navigating across different pages




 

51. What is React?

Ans:

React is a front end JavaScript library developed by Facebook in 2011. It follows the component based approach which helps in building reusable UI components. It is used for developing complex and interactive web and mobile UI. Even though, it was open-sourced only in 2015, it has one of the largest communities supporting it.

52. What are the features of React?

Ans:

JSX: JSX is JavaScript syntax extension.
Components : React is all about components.
One direction flow: React implements one way data flow which makes it easy to reason about your app

53. List some of the major advantages of React.

Ans:

Some of the major advantages of React are:

  • It increases the application’s performance
  • It can be used on client and server side
  • Code’s readability increases, because of JSX.
  • It is easy to integrate with other frameworks such as Angular, Meteor etc
  • Using React, writing UI test cases become extremely easy
  • React uses virtual DOM which is JavaScript object.
  • This will improve apps performance
  • Component and Data patterns improve readability.

54. What are the limitations of React?

Ans:

Limitations of React are listed below:

  • React is just a library, not a full-blown framework
  • Its library is very large and takes time to understand
  • It can be little difficult for the novice programmers to understand
  • Coding gets complex as it uses inline templating and JSX

55. What is JSX?

Ans:

JSX is a shorthand for JavaScript XML. This is a type of file used by React which utilizes the expressiveness of JavaScript along with HTML like template syntax. This makes the HTML file really easy to understand. This file makes applications robust and boosts its performance. Below is an example of JSX:

render(){
return(
<div>
<h1> Welcome To Tekslate!!</h1>
</div>
);
}

56. Differentiate between Real DOM and Virtual DOM.

Ans:

Real DOM

Virtual DOM

It updates slowly.

It updates faster.

Can directly update HTML.

Can’t directly update HTML.

Creates a new DOM if element updates.

Updates the JSX if element updates.

DOM manipulation is very expensive.

DOM manipulation is very easy.

Too much of memory wastage.

No memory wastage.

57. Real DOM vs Virtual DOM

Ans:

Browsers can only read JavaScript objects but JSX in not a regular JavaScript object. Thus to enable a browser to read JSX, first, we need to transform JSX file into a JavaScript object using JSX transformers like Babel and then pass it to the browser.

58. How is React different from Angular?

Ans:

TOPIC

REACT

ANGULAR

ARCHITECTURE

Only the View of MVC

Complete MVC

RENDERING

Server side rendering

Client side rendering

DOM

Uses virtual DOM

Uses real DOM

DATA BINDING

One-way data binding

Two-way data binding

DEBUGGING

Compile time debugging

Run time debugging

AUTHOR

Facebook

Google

59. Why can’t browsers read JSX?

Ans:

Components are the building blocks of a React application’s UI. These components split up the entire UI into small independent and reusable pieces. Then it renders each of these components independent of each other without affecting the rest of the UI.

60. Explain the purpose of render() in React.

Ans:

Each React component must have a render() compulsory. If more than one HTML elements needs to be rendered, then they must be grouped together inside one enclosing tag such as <form>, <group>,<div> etc. It returns to the single react element which is the presentation of native DOM Component. This function must be kept pure i.e., it must return the same result each time it is invoked.


 

61. What is Props?

Ans:

Props are short hand for Properties in React. They are read-only components which must be kept pure i.e. immutable. They are always passed down from the parent to the child components through out the application. A child component can never send a prop back to the parent component. This help in maintaining the unidirectional data flow and are generally used to render the dynamically generated data.

62. What is a state in React and how is it used?

Ans:

States are the heart of React components. States are the source of data and must be kept as simple as possible. Basically, states are the objects which determine components rendering and behavior. They are mutable unlike the props and create dynamic and interactive components. They are accessed via this.state().

63. Differentiate states and props.

Ans:

Conditions

State

Props

1. Receive initial value from parent component

Yes

Yes

2. Parent component can change value

No

Yes

3. Set default values inside component

Yes

Yes

4. Changes inside component

Yes

No

5. Set initial value for child components

Yes

Yes

6. Changes inside child components

No

Yes

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

Ans:

Arrow functions are more of brief syntax for writing the function expression. They are also called ‘fat arrow‘ (=>) the functions. These functions allow to bind the context of the components properly since in ES6 auto binding is not available by default. Arrow functions are mostly useful while working with the higher order functions.

//General way
render() {
return(
<MyInput onChange={this.handleChange.bind(this) } />
);
}
//With Arrow Function
render() {
return(
<MyInput onChange={ (e) => this.handleOnChange(e) } />
);
}

65. Differentiate between stateful and stateless components.

Ans:

Stateful Component

Stateful Component

Stores info about component’s state change in memory

Stores info about component’s state change in memory

Have authority to change state

Do not have the authority to change state

Contains the knowledge of past, current and possible future changes in state

Contains no knowledge of past, current and possible future state changes

Stateless components notify them about the requirement of the state change, then they send down the props to them.

They receive the props from the Stateful components and treat them as callback functions.

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

Ans:

There are three different phases of React component’s lifecycle:

Initial Rendering Phase: This is the phase when the component is about to start its life journey and make its way to the DOM.
Updating Phase: Once the component gets added to the DOM, it can potentially update and re-render only when a prop or state change occurs. That happens only in this phase.
Unmounting Phase: This is the final phase of a component’s life cycle in which the component is destroyed and removed from the DOM.

67. Explain the lifecycle methods of React components in detail.

Ans:

Some of the most important lifecycle methods are:
componentWillMount() – Executed just before rendering takes place both on the client as well as server-side.
componentDidMount() – Executed on the client side only after the first render.
componentWillReceiveProps() – Invoked as soon as the props are received from the parent class and before another render is called.
shouldComponentUpdate() – Returns true or false value based on certain conditions. If you want your component to update, return true else return false. By default, it returns false.
componentWillUpdate() – Called just before rendering takes place in the DOM.
componentDidUpdate() – Called immediately after rendering takes place.
componentWillUnmount() – Called after the component is unmounted from the DOM. It is used to clear up the memory spaces.

68. What is an event in React?

Ans:

In React, events are the triggered reactions to specific actions like mouse hover, mouse click, key press, etc. Handling these events are similar to handling events in DOM elements. But there are some syntactical differences like:
Events are named using camel case instead of just using the lowercase.
Events are passed as functions instead of strings.
The event argument contains a set of properties, which are specific to an event. Each event type contains its own properties and behavior which can be accessed via its event handler only.

69. What are synthetic events in React?

Ans:

Synthetic events are the objects which act as a cross-browser wrapper around the browser’s native event. They combine the behavior of different browsers into one API. This is done to make sure that the events show consistent properties across different browsers.

70. List some of the cases when you should use Refs.

Ans:

Following are the cases when refs should be used:

  • When you need to manage focus, select text or media playback
  • To trigger imperative animations
  • Integrate with third-party DOM libraries


 

71. What do you know about controlled and uncontrolled components?

Ans:

Controlled Components

Uncontrolled Components

They do not maintain their own state

They maintain their own state

Data is controlled by the parent component

Data is controlled by the DOM

They take in the current values through

props and then notify the changes via callbacks.

Refs are used to get their current values

72. What are Higher Order Components(HOC)?

Ans:

Higher Order Component is an advanced way of reusing the component logic. Basically, it’s a pattern that is derived from React’s compositional nature. HOC are custom components which wraps another component within it. They can accept any dynamically provided child component but they won’t modify or copy any behavior from their input components. You can say that HOC are ‘pure’ components.

73. What can you do with HOC?

Ans:

HOC can be used for many tasks like:

  • Code reuse, logic and bootstrap abstraction
  • Render High jacking
  • State abstraction and manipulation
  • Props manipulation

74. What are Pure Components?

Ans:

Pure components are the simplest and fastest components which can be written. They can replace any component which only has a render(). These components enhance the simplicity of the code and performance of the application.

75. What is the significance of keys in React?

Ans:

Keys are used for identifying unique Virtual DOM Elements with their corresponding data driving the UI. They help React to optimize the rendering by recycling all the existing elements in the DOM. These keys must be a unique number or string, using which React just reorders the elements instead of re-rendering them. This leads to increase in application’s performance.

76. What were the major problems with MVC framework?

Ans:

Following are some of the major problems with MVC framework:

  • DOM manipulation was very expensive
  • Applications were slow and inefficient
  • There was huge memory wastage
  • Because of circular dependencies, complicated model was created around models and views

77. What is Redux?

Ans:

Redux is one of the hottest libraries for front end development in today’s marketplace. It is a predictable state container for JavaScript applications and is used for the entire applications state management. Applications developed with Redux are easy to test and can run in different environments showing consistent behavior.

78. What do you understand by “Single source of truth”?

Ans:

Redux uses ‘Store’ for storing the application’s entire state at one place. So all the component’s state are stored in the Store and they receive updates from the Store itself. The single state tree makes it easier to keep track of changes over time and debug or inspect the application.

79. List down the components of Redux.

Ans:

Redux is composed of the following components:
Action – It’s an object that describes what happened.
Reducer – It is a place to determine how the state will change.
Store – State/ Object tree of the entire application is saved in the Store.
View – Simply displays the data provided by the Store.

80. How are Actions defined in Redux?

Ans:

Actions in React must have a type property that indicates the type of ACTION being performed. They must be defined as a String constant and you can add more properties to it as well. In Redux, actions are created using the functions called Action Creators.
Below is an example of Action and Action Creator:

function addTodo(text) {
return {
type: ADD_TODO,
text
}
}



 

81. Explain the role of Reducer.

Ans:

Reducers are pure functions which specify how the application’s state changes in response to an ACTION. Reducers work by taking in the previous state and action, and then it returns a new state. It determines what sort of update needs to be done based on the type of the action, and then returns new values. It returns the previous state as it is, if no work needs to be done.

82. What is the significance of Store in Redux?

Ans:

A store is a JavaScript object which can hold the application’s state and provide a few helper methods to access the state, dispatch actions and register listeners. The entire state/ object tree of an application is saved in a single store. As a result of this, Redux is very simple and predictable. We can pass middleware to the store to handle processing of data as well as to keep a log of various actions that change the state of stores. All the actions return a new state via reducers.

83. How is Redux different from Flux?

Ans:

Flux

Redux

The Store contains state and change logic

Store and change logic are separate

There are multiple stores

There is only one store

All the stores are disconnected and flat

Single store with hierarchical reducers

Has singleton dispatcher

No concept of dispatcher

React components subscribe to the store

Container components utilize connect

State is mutable

State is immutable

84. What are the advantages of Redux?

Ans:

Advantages of Redux are listed below:
Predictability of outcome: Since there is always one source of truth, i.e. the store, there is no confusion about how to sync the current state with actions and other parts of the application.
Maintainability: It is simple to maintain with a strict structure and predictable outcome.
Server side rendering: To the client side, You just need to pass the store created on the server. This is helpful for initial render and provides a high quality user experience as it optimizes the application performance.
Developer tools: From actions to state changes, developers can track everything going on in the application in real time.
Community and ecosystem: Redux has a huge community behind it which makes it even more captivating to use. A large community of talented individuals contribute to the betterment of the library and develop various applications with it.
Ease of testing: Redux’s code is mostly functions which are small, pure and isolated. This makes the code testable and independent.
Organization: Redux is precise about how code should be organized, this makes the code more consistent and easier when a team works with it.

85. What is React Router?

Ans:

React Router is a powerful routing library built on top of React, which helps in adding new screens and flows to the application. This keeps the URL in sync with data that’s being displayed on the web page. It maintains a standardized structure and behavior and is used for developing single page web applications. React Router has a simple API.

86. Why is switch keyword used in React Router v4?

Ans:

Although a <div> is used to encapsulate multiple routes inside the Router. The ‘switch’ keyword is used when you want to display only a single route to be rendered amongst the several defined routes. The <switch> tag when in use matches the typed URL with the defined routes in sequential order. When the first match is found, it renders the specified route. Thereby bypassing the remaining routes.

87. Why do we need a Router in React?

Ans:

A Router is used to define multiple routes and when a user types a specific URL, if this URL matches the path of any ‘route’ defined inside the router, then the user is redirected to that particular route. So basically, we need to add a Router library to our app that allows creating multiple routes with each leading to us a unique view.

<switch>
<route exact path=’/’ component={Home}/>
<route path=’/posts/:id’ component={Newpost}/>
<route path=’/posts’ component={Post}/>
</switch>

88. List down the advantages of React Router.

Ans:

Few advantages are:
Just like how React is based on components, in React Router v4, the API is ‘All About Components’. A Router can be visualized as a single root component (<BrowserRouter>) in which we enclose the specific child routes (<route>).
No need to manually set History value: In React Router v4, all we need to do is wrap our routes within the<BrowserRouter> component.
The packages are split: Three packages one each for Web, Native and Core. This supports the compact size of our application. It’s easy to switch over based on similar coding style.

89. How is React Router different from conventional routing?

Ans:

Topic

Conventional Routing

React Routing

PAGES INVOLVED

Each view corresponds to a new file

Only single HTML page is involved

URL CHANGES

A HTTP request is sent to server and corresponding HTML page is received

Only the History attribute is changed

FEEL

User actually navigates across different pages for each view

User is duped thinking he is navigating across different pages

90. What is React?

Ans:

React is an open-source JavaScript library developed by Facebook Engineers for building complex and interactive User Interfaces in web & mobile applications.


 

91. How is React different?

Ans:

Since React is a little small concentrated on building UI components, it is essentially not the same as a considerable measure of other JavaScript frameworks.
For instance, AngularJS (1.x) approaches building an application by expanding HTML markup and infusing different develops (e.g. Directives, Controllers, Services) at runtime. Subsequently, AngularJS is extremely obstinate about the more architecture design of your application — these reflections are absolutely helpful now and again, yet much of the time, they come at the cost of adaptability.

92. Why ReactJS is used?

Ans:

React is used to handle the view part of Mobile application and Web application.

93. How ReactJS framework is different as compared to others?

Ans:

Basically, ReactJS is a limited library that builds UI parts, it is essentially not quite the same as a considerable measure of other JavaScript structures. One common example is AngularJS approaches building an app simply by expanding HTML markup and infusing different develop such as controller at runtime. Therefore, AngularJS is exceptionally obstinate about the more noteworthy engineering of your application.

94. Does ReactJS use HTML?

Ans:

No, It uses JSX which is simiar to HTM.

95. What do you know about the component lifecycle in ReactJS?

Ans:

Component lifecycle is an essential part of this platform. Basically, they have lifecycle events that fall in the three prime categories which are property updates, Initialization and third are Destruction. They are generally considered as a method of simply managing the state and properties of every reach component.

96. What do you mean by ReactJS?

Ans:

It is nothing but a JavaScript library which was developed by the popular social media giant facebook. The prime aim was to build user interfaces. The good thing is that developers can simply create renewable UI components. There are certain factors that have contributed to its success and the one among them is the support of a very large community.

97. What are the life Cycle of ReactJS?

Ans:

  • Initialization
  • State/Property Updates
  • Destruction

98. When ReactJS released?

Ans:

March 2013

99. How is ReactJs different from AngularJS?

Ans:

The first difference between both of them is their code dependency. ReactJS depends less to the code whereas AngularJS needs a lot of coding to be done. The packaging on React is quite strong as compared to the AngularJS. Another difference is React is equipped with Virtual Dom while the Angular has a Regular DOM. ReactJS is all about the components whereas AngularJS focus mainly on the Models, View as well as on Controllers. AngularJS was developed by Google while the ReactJS is the outcome of facebook. These are some of the common differences between the two.

100. What do you mean by Redux?

Ans:

Many times there is a need to handle the data of an app in a reliable manner. For such tasks, Redux is used. It accurately performs its task and always makes sure that the entire data has been controlled. It is also possible to apply filters in case only a specific part of data is required.



 

101. What do you know about Flux?

Ans:

Basically, Flux is a basic illustration that is helpful in maintaining unidirectional data stream. It is meant to control construed data unique fragments to make them interface with that data without creating issues. Flux configuration is insipid; it’s not specific to React applications, nor is it required to collect a React application. Flux is basically a straightforward idea, however in you have to exhibit a profound comprehension of its usage.

102. What is current stable version of ReactJS?

Ans:

Version: 15.5
Release on: April 7, 2017

103. What is Repository URL of ReactJS?

Ans:

https://github.com/facebook/react

104. What is JSX?

Ans:

It is basically a novel dialect of the popular JavaScript that simply integrates the HTML templates into the code of JavaScript. The browser is not capable to read the code simply and thus there is a need for this integration. Generally, WebPack or Babel tools are considered for this task. It has become a very popular approach in the present scenario among the developers.

105. Do you think ReactJS has any limitations? If so, tell a few?

Ans:

Yes, there are a few drawbacks which are associated with this platform. The leading drawback of the ReactJS is the size of its library. It is very complex and creates a lot of confusion among the developers. Also, there are lots of developers all over the world which really don’t like the JSX and inline templating. In addition to this, there is another major limitation of ReactJS and i.e. only cover one layer of the app and i.e.View. Thus to manage the development, developers have to depend on several other technologies which consume time.

106. What are the feature of ReactJS?

Ans:

JSX
Components
One direction flow (Unidirectional Flux)

107. What are the Advantages of ReactJS?

Ans:

  • Uses virtual DOM which is JavaScript object which will improve apps performance.
  • Can be used on client and server side
  • Component and Data patterns improve readability.
  • Improves SEO Performance
  • Can be used with other framework also.

108. How the parent and child components exchange information?

Ans:

This task is generally performed with the help of functions. Actually, there are several functions which are provided to both parent and child components. They simply make use of them through props. Their communication should be accurate and reliable. The need of same can be there anytime and therefore functions are considered for this task. They always make sure that information can be exchanged easily and in an efficient manner among the parent and child components.

109. Give one basic difference between pros and state?

Ans:

Pros are immutable while the state is mutable. Both of them can update themselves easily.

110. How do you tell React to build in Production mode and what will that do?

Ans:

Ordinarily you’d utilize Webpack’s DefinePlugin strategy to set NODE_ENV to production. This will strip out things like propType approval and additional notices. Over that, it’s likewise a smart thought to minify your code in light of the fact that React utilizes Uglify’s dead-code end to strip out advancement just code and remarks, which will radically diminish the measure of your package.




 

111. What do you understand with the term polling?

Ans:

The server needs to be monitored to for updates with respect to time. The primary aim in most of the cases is to check whether novel comments are there or not. This process is basically considered as pooling. It checks for the updates approximately after every 5 seconds. It is possible to change this time period easily. Pooling help keeping an eye on the users and always make sure that no negative information is present on the servers. Actually, it can create issues related to several things and thus pooling is considered.

112. When would you use a Class Component over a Functional Component?

Ans:

If your component has state or a lifecycle method(s), use a Class component. or else, use a Functional component.

113. What do you mean by virtual DOM?

Ans:

For all the available DOM objects in ReactJS, there is a parallel virtual DOM object. It is nothing but can be considered as the lighter version of the true copy and is powerful in eliminating the complex code. It is also used as a Blue Print for performing several basic experiments. Many developers also use it while practicing this technology.

114. Compare MVC with Flux?

Ans:

MVC approaches are presently considered as outdated. Although they are capable to handle data concerns, controllers as well as UI, many developers found that it doesn’t properly work when applications size increases. However, they are capable to handle some of the key issues such as eliminating the lack of data integrity as well as managing the data flow which is not properly defined. On the other side, Flux works perfectly with all the sizes irrespective of their size.

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

Ans:

Basically, a React component describes what you need to see on the screen. Not all that basically, a React element is a protest portrayal of some UI.
A React component is a function or a class which alternatively acknowledges input and returns a React component (ordinarily by means of JSX which gets transpiled to a createElement invocation).

116. Tell us three reasons behind the success of ReactJS?

Ans:

ReactJS is a technology that can be trusted for complex tasks. While performing any task through it, developers need not worry about the bugs. It always ensures error free outcomes and the best part is it offers scalable apps. It is very fast technology and can simply be trusted for quality outcomes.

117. In which lifecycle event do you make AJAX requests and why?

Ans:

AJAX solicitations ought to go in the componentDidMount lifecycle event.
There are a couple of reasons behind this,
Fiber, the following usage of React’s reconciliation algorithm, will be able to begin and quit rendering as required for execution benefits. One of the exchange offs of this is componentWillMount, the other lifecycle event where it may bode well to influence an AJAX to ask for, will be “non-deterministic”. This means React may begin calling componentWillMount at different circumstances at whenever point it senses that it needs to. This would clearly be a bad formula for AJAX requests.
You can’t ensure the AJAX request won’t resolve before the component mounts. In the event that it did, that would imply that you’d be attempting to setState on an unmounted component, which won’t work, as well as React will holler at you for. Doing AJAX in componentDidMount will ensure that there’s a component to update.

118. What is the difference between createElement and cloneElement?

Ans:

createElement is the thing that JSX gets transpiled to and is the thing that React uses to make React Elements (protest representations of some UI). cloneElement is utilized as a part of request to clone a component and pass it new props. They nailed the naming on these two.

119. What do you mean by prop?

Ans:

ReactJS is equipped with a very amazing feature. It enables developers to simply add our attributes while using the defined components. These attributes are commonly called as props. They are used for rendering the dynamic data and using them is not at all a big deal. Developers can save a lot of time and eliminates the chances of occurrence of bugs and errors by using Prop.

120. What is meant by event handling?

Ans:

To capture the user’s information and other similar data, event handling system is considered. It is generally done through DOM elements which are present in the code. This task is simple to accomplish. Two-way communication is considered in this approach.


 

121. How many outermost elements can be there in a JSX expression?

Ans:

It must have one JSX element present so that the task can be accomplished easily. Having more than one expression is not an issue but probably it will slow down the process. There are also chances of confusion with more than one expression if you are new to this technology.

122. What are controlled and uncontrolled components?

Ans:

There are components in the ReactJS that maintain their own internal state. They are basically considered as uncontrolled components. On the other side, the components which don’t maintain any internal state are considered as controlled components in ReactJS. Controlled components can easily be controlled by several methods. Most of the React components are controlled components.

123. Mention the key benefits of Flux?

Ans:

Applications that are built on Flux have components which can simply be tested. By simply updating the store, developers are able to manage and test any react component. It cut down the overall risk of data affection. All the applications are highly scalable and suffer no compatibility issues.

124. Why browsers cannot read JSX?

Ans:

Actually, JSX is not considered as a proper JavaScript. Browsers cannot read it simply. There is always a need to compile the files that contain JavaScript Code. This is usually done with the help of JSX compiler which performs its task prior to file entering the browser. Also, compiling is not possible in every case. It depends on a lot of factors such as source or nature of file or data.

125. How DOM and Virtual Dom object are different from one another?

Ans:

Virtual DOM is not capable to affect the HTML directly. As compared to a regular DOM, Virtual is quite faster and can perform its task reliably. In addition to this, Virtual Dome is capable to automate itself. Also, Virtual DOM is capable to handle more tasks without facing any of the issues.

126. What happens during the lifecycle of a React component?

Ans:

A standout amongst the most valuable parts of React is its segment lifecycle — so seeing precisely how segments components after some time is instrumental in building a viable application.

127. What exactly you can do if the expression contains more than one line?

Ans:

In such a situation, enclosing the multi-line JSX expression is an option. If you are a first time user, it may seem awkward but later you can understand everything very easily. Many times it becomes necessary to avoid multi-lines to perform the task reliably and for getting the results as expected.

128. What do you know about React Router?

Ans:

Rendering the components is an important task in ReactJS. React router is used to decide which components is to be rendered and which one should not. It also performs dictation during several activities.

129. Compare Flux vs MVC

Ans:

Conventional MVC designs have functioned admirably to separate the worries of data (Model), UI (View) and logic (Controller) — however many web engineers have found impediments with that approach as applications develop in measure. In particular, MVC architectures as often as possible experience 2 primary issues:
Ineffectively defined data flow: The cascading updates which happen crosswise over perspectives frequently prompt a tangled web of events which is hard to debug.
Lack of data integrity: Model data can be changed from anyplace, yielding erratic results over the UI.
With the Flux pattern complex UIs never again experience the ill effects of cascading updates; any given React component will have the capacity to recreate its state in light of the information gave by the store. The flux pattern likewise upholds data integrity by limiting direct access to the shared data.
While a technical interview, it is awesome to talk about the contrasts between the Flux and MVC configuration designs inside the setting of a particular illustration:
For instance, imagine we have an “master/detail” UI in which the client can choose a record from a rundown (master view) and alter it utilizing an auto-populated form (detail view).
With a MVC architecture, the data contained inside the Model is shared between both the master and detail Views. Each of these perspectives may have its own particular Controller assigning updates between the Model and the View. Anytime the information contained inside the Model may be updated — and it’s hard to know where precisely that change happened. Did it occur in one of the Views sharing that Model, or in one of the Controllers? Since the Model’s information can be transformed by any performing artist in the application, the danger of information contamination in complex UIs is more prominent than we’d like.
With a Flux architecture, the Store data is correspondingly shared between different Views. However this data can’t be straightforwardly changed — the greater part of the solicitations to update the data must go through the Action > Dispatcher chain first, eliminating of the risk of arbitrary data pollution. At the point when refreshes are made to the data , it’s presently significantly less demanding to find the code requesting for those progressions.

130. What is one of the core types in React?

Ans:

ReactNode



 

131. What do you mean by the state?

Ans:

It is basically a JavaScript object that is meant to effectively handle the changes into the data. Generally, it is present inside in all the components that are used. It is considered as an important part of RectJS apps which boost user interfaces. As it represents the data that change over the time, certain errors can be eliminated and developers can always ensure quality.

132. What is redux?

Ans:

A method os handling the state (or data) of an application.

133. Is it possible to display props on a parent component?

Ans:

Yes, it is possible. The best way to perform this task is using spread operator. It can also be done with listing the properties but this is a complex process.

134. In ReactJS, why there is a need to capitalize the components?

Ans:

It is necessary because components are not the DOM element but they are constructors. If they are not capitalized, they can cause various issues and can confuse developers with several elements. At the same time, the problem of integration of some elements and commands can be there.

135. What do you know about synthetic events?

Ans:

ReactJS is capable to simply transform original events of browsers by monitoring the behavior of browser. This is done to make sure that events have logical properties beyond the browsers that are different. Actually, they act as cross browser envelope around the logical event system of the browser.

136. Explain DOM diffing?

Ans:

When the components are rendered twice, Virtual Dom begins checking the modifications elements have got. They represent the changed element on the page simply. There are several other elements that don’t go through changes. To cut down the changes to the DOM as an outcome of user activities, DOM doffing is considered. It is generally done to boost the performance of the browser. This is the reason for its ability to perform all the tasks quickly.

137. Is it possible to nest JSX elements into other JSX elements?

Ans:

It is possible. The process is quite similar to that of nesting the HTML elements. However, there are certain things that are different in this. You must be familiar with the source and destination elements to perform this task simply.