preloader
React Interview Questions

50 React Interview Questions and Answers for Preparation

author image

React is an important part of javascript so this is a very common topic to be asked in technical questions related to JS. This page will help you to prepare for React Interview Questions. Here we have brought 50+ React Interview questions and answers of different levels for freshers and experienced candidates. So to ace, your job interview prepare from this page.

About React: It is a JavaScript library used for developing user interfaces with declarative, efficient, and flexible features. It works for View “V” in MVC. It is a component-based front-end library that is open-source and works only for the view layer of the application. It is developed and managed by Facebook

React Interview Questions

1. Explain the difference between Virtual DOM and Real DOM.

2. Explain React?

3. Tell some features of React?

4. Tell me some advantages of React.

5. Tell me some limitations of React?

6. Explain JSX?

7. Explain Virtual DOM and its works.

8. Why can’t browsers read JSX?

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

10. How is React different from Angular?

11. Explain the statement “In React, everything is a component.”

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

13. Show code of embedding two or more components into one?

14. Explain Props?

15. Explain a state in React and its usage?

16. What is the difference between states and props?

17. Show the code to update the state of a component?

18. Explain the arrow function in React and its usage?

19. What is the difference between stateful and stateless components?

20. Explain different phases of React component’s lifecycle?

21. Describe the lifecycle methods of React components.

22. Explain an event in React?

23. Show the coding to create an event in React?

24. Explain synthetic events in React?

25. Explain refs in React?

26. Provide some cases where you use Refs.

27. Tell the ways to modularize code in React?

28. How do you create forms in React?

29. Explain controlled and uncontrolled components?

30. Explain Higher-Order Components (HOC)?

31. What can you use HOC for?

32. Explain Pure Components?

33. Tell the significance of keys in React?

34. Tell me some major problems with the MVC framework?

35. What is Flux?

36. Explain Redux?

37. Explain the 3 principles that Redux follows?

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

39. Explain the components of Redux.

40. Define Actions in Redux?

41. What is the role of the Reducer?

42. Tell the significance of the Store in Redux?

43. Differentiate Redux and Flux?

44. Tell some advantages of Redux?

45. Explain React Router?

46. Why is the switch keyword used in React Router v4?

47. Explain the need for a Router in React?

48. Tell the advantages of React Router.

49. Differentiate between React Router and conventional routing?


Follow Simple Steps to apply in these MNC’s:


React Interview Questions and Answers

1. Explain the difference between Virtual DOM and Real DOM.

  • Real DOM updates slowly while Virtual DOM updates faster.
  • Real DOM directly updates HTML whereas Virtual DOM cannot
  • In Real DOM manipulation is very expensive and in Virtual DOM it is easy
  • If element updates Real DOM creates a new DOM and Virtual DOM Updates the JSX.
  • Real DOM does Too much memory wastage whereas Virtual DOM wastes no memory.

2. Explain React?

In 2011 Facebook developed a front-end JavaScript library which is named React. It helps in building the reusable UI components that follow the component-based approach. It makes complex and interactive UI for mobile and web.

3. Tell some features of React?

  1. It majorly uses virtual DOM rather than real DOM.
  2. It uses server-side rendering.
  3. It works on unidirectional data flow or data binding.

4. Tell me some advantages of React.

  1. It enhances the application’s performance
  2. It can use both i.e. client as well as server-side
  3. JSX improves code’s readability
  4. It easily integrates with other frameworks like Angular, Meteor, etc
  5. React makes writing UI test cases very easy

5. Tell me some limitations of React?

  1. It is not a full-blown framework but just a library.
  2. The library is bulky and consumes time in understanding.
  3. For beginner programmers, it is a little difficult to understand.
  4. Inline templating and JSX makes the coding complex.

6. Explain JSX?

It is basically shorthand for XML JavaScript. React use these files to utilize the expressiveness of JavaScript additionally with HTML-like template syntax. It makes the HTML file coding very easy to understand. JSX improves the performance of applications and makes them robust. For example:

render(){

return(

Hello World from JobInterviewNinjas!!

);

}

7. Explain Virtual DOM and its works.

It is a lightweight JavaScript object which in reality is a copy of the real DOM. It is a node tree that makes the lists of all the elements, attributes used, and content as Objects with their properties. React render function is used to develop a node tree out of the React components. After that, it updates the node tree in reply to the mutations in the data model this is due to the various actions done by the user or system.

These are the three simple steps of Virtual DOM working.

  1. Whenever there are changes in underlying data, the entire UI is re-rendered.
  2. Then it calculates the difference between the previous DOM and the new one.
  3. After completing the calculations the real DOM is updated with only those parts that have changed.

8. Why can’t browsers read JSX?

Browsers are only able to read JavaScript objects and JSX is not a JavaScript object. So to make a browser read JSX, we need to convert a JSX file into a JavaScript object by using JSX transformers such as Babel, and then the browser can read it.

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

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

Hello JobInterviewNinjas!

;

}

});

// ES6

class MyComponent extends React.Component {

render() {

return

Hello JobInterviewNinjas!

;

}

}

Props

// ES5

var App = React.createClass({

propTypes: { name: React.PropTypes.string },

render: function() {

return

Hello, {this.props.name}!

;

}

});

// ES6

class App extends React.Component {

render() {

return

Hello, {this.props.name}!

;

}

}

State

// ES5

var App = React.createClass({

getInitialState: function() {

return { name: ‘world’ };

},

render: function() {

return

Hello, {this.state.name}!

;

}

});

// ES6

class App extends React.Component {

constructor() {

super();

this.state = { name: ‘world’ };

}

render() {

return

Hello, {this.state.name}!

;

}

}

10. How is React different from Angular?

Architecture: React is the view part of MVC whereas Angular is a complete MVC.

Rendering: In React it is server-side rendering whereas in Angular it is client-side.

DOM: React uses virtual DOM and Angular uses real DOM.

Data Binding: React uses one-way data binding and Angular uses two-way data binding.

Debugging: React does compile-time debugging and Angular use Runtime Debugging.

Author: React is developed by Facebook and Angular is made by Google.

11. Explain the statement “In React, everything is a component.”

In React application’s UI, Components are the building blocks. These components divide the whole UI into small independent and reusable pieces. Then it individually works on these independent components without disturbing the rest of the UI.

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

All the React components have a render(). It brings back a single React element that represents the native DOM component. If you render two or more HTML elements, then they need to be grouped together inside enclosed in one tag such as
, ,
etc. It is a pure function that means it must bring back the same result every time it is invoked.

13. Show code of embedding two or more components into one?

class MyComponent extends React.Component{

render(){

return(

Hello

);

}

}

class Header extends React.Component{

render(){

return

Header Component

};

}

ReactDOM.render(

, document.getElementById(‘content’)

);

14. Explain Props?

In React, Props is a shorthand property and are read-only components that need to be pure i.e. immutable. They always move from parent to child components throughout the application. A child component is not allowed to send a prop back to the parent. This gives the unidirectional data flow and is used to render the data dynamically.

15. Explain a state in React and its usage?

States are called the heart of React components. States are data sources that need to be kept as simple as possible. Mostly, states are the objects which regulate components rendering and behavior. They are mutable and make dynamic and interactive components. They are accessed via this.state().

16. What is the difference between states and props?

| — | — | — | | Conditions | State | Props | | Receive initial value from parent component | Yes | Yes | | Parent component can change value | No | Yes | | Set default values inside component | Yes | Yes | | Changes inside component | Yes | No | | Set initial value for child components | Yes | Yes | | Changes inside child components | No | Yes |

17. Show the code to update the state of a component?

this.setState() this function is used to state a component. 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. Explain the arrow function in React and its usage?

It is a kind of brief syntax for writing the function expression. Arrow function is also known as ‘fat arrow’ (=>) the function. This function helps in binding the context of the components since in ES6 when the auto binding is not a default function. To work with higher-order functions, arrow functions are most useful.

//General way

render() {

return(

<MyInput onChange={this.handleChange.bind(this) } />

);

}

//With Arrow Function

render() {

return(

<MyInput onChange={ (e) => this.handleOnChange(e) } />

);

}

19. What is the difference between stateful and stateless components?

Stateful Component:

  • Stores info related to component’s state change in memory
  • Have the ability to change state
  • Know past, current, and possible future changes in state
  • Stateless components inform them related to requirements of the state change and send props to them.

Stateless Component:

  1. It is used to calculate the internal state of the component
  2. They cannot change state
  3. Not know about past, current, and possible future state changes
  4. They collect the props from the Stateful components and take them as callback functions.

20. Explain different phases of React component’s lifecycle?

  • Initial Rendering Phase: When a component is about to start its journey and make its path to the DOM.
  • Updating Phase: Once the component is added to the DOM, then it can update and re-render only when there is a change in a prop or state.
  • Unmounting Phase: This is the last phase in which the component is destroyed and detached from the DOM.

Interview Questions On React

21. Describe the lifecycle methods of React components.

  • componentWillMount() – This component is executed before rendering starts in both client as well as server-side.
  • componentDidMount() – This component is executed on the client-side when the first render takes place.
  • componentWillReceiveProps() – This component is invoked when the parent class receives props before another render is called.
  • shouldComponentUpdate() – Returns with true or false value depending on conditions. If your component is updated then it returns true unless return false. By default, it returns false.
  • componentWillUpdate() – This component is called before rendering starts in the DOM.
  • componentDidUpdate() – This component is called immediately after rendering takes place.
  • componentWillUnmount() – This component is called after the unmounting of components from the DOM. This frees the memory spaces.

22. Explain an event in React?

Events are the triggered reactions of actions like a mouse click, mouse hover, keypress, etc. But there are some differences like:

  1. Events are named using camel case rather than lowercase.
  2. Events are passed as functions rather than strings.

The event argument has a set of properties, which are definite to an event. Each event type has its property and behavior which can be executed via using the event handler.

23. Show the coding to create an event in React?

class Display extends React.Component({

show(evt) {

// code

},

render() {

// Render the div with an onClick prop (value is a function)

return (

Click Me!

);

}

});

24. Explain synthetic events in React?

These are the objects which work as a cross-browser wrapper around the browser’s native event. They merge the behavior of various browsers into one API. This is to ensure that the events show constant properties across different browsers.

25. Explain refs in React?

Refs are the short term for References in React. It is an attribute that stores a reference to a specific React element or component, which will be brought back by the components render configuration function. It is used to bring back the references to a specific element or component returned by render(). They are handy when DOM measurements are required or where we need to add methods to the components.

class ReferenceDemo extends React.Component{

display() {

const name = this.inputDemo.value;

document.getElementById(‘disp’).innerHTML = name;

}

render() {

return(

Name: <input type="text” ref={input => this.inputDemo = input} />

Hello !!!

);

}

}

26. Provide some cases where you use Refs.

  • When you require to manage focus, select text or media playback
  • To activate imperative animations
  • Incorporate with third-party DOM libraries

27. Tell the ways to modularize code in React?

By using import and export properties we can modularize code in React. They allow us to write the components distinctly in different files.

//ChildComponent.jsx

export default class ChildComponent extends React.Component {

render() {

return(

This is a child component

);

}

}

//ParentComponent.jsx

import ChildComponent from ‘./childcomponent.js’;

class ParentComponent extends React.Component {

render() {

return(

);

}

}

28. How do you create forms in React?

React forms are the same as HTML forms. The difference is in React, the state is enclosed in the state property of the component and can only be updated by using setState(). The elements are not able to update their state themselves but are handled by a JavaScript function. This function has complete access to the data that is given by the user into a form.

handleSubmit(event) {

alert(‘A name was submitted: ' + this.state.value);

event.preventDefault();

}

render() {

return (

);

}

29. Explain controlled and uncontrolled components?

Controlled Components:

  1. They don’t maintain their state
  2. Parent component control the Data
  3. They receive the current values through props and then inform the changes via callbacks

Uncontrolled Components:

  1. They maintain their state
  2. DOM control the Data
  3. Refs are used to fetching their current values

30. Explain Higher-Order Components (HOC)?

It is an innovative way of reusing the component logic. It’s just a pattern that is derivative from React’s compositional nature. Higher-Order Components are custom components that cover another component within it. They can take any dynamically provided child component but they cannot modify or copy any behavior from their input components. You can say that Higher-Order Components (HOC) are ‘pure’ components.

React Interview Questions For Experienced

31. What can you use HOC for?

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

32. Explain Pure Components?

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

33. Tell the significance of keys in React?

Keys are used to identifying unique Virtual DOM Elements with their consistent data driving the UI. They help React to well-manage the rendering by reutilizing all the current 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 enhancing the application’s performance.

34. Tell me some major problems with the MVC framework?

  • DOM manipulation is highly expensive
  • Applications run slow and are not efficient
  • There make huge memory wastage
  • Because of circular dependencies, a complex model is created around models and views

35. What is Flux?

It is a structured pattern that imposes the unidirectional data flow. It manages derived data and allows communication between multiple components by only using a central Store that has access to all data. Any update or changes in data throughout the application must occur in flux only. It gives stability to the application and decreases run-time errors.

36. Explain Redux?

It is one of the most popular libraries used for front-end development. It is an expected state container for JavaScript applications that are used for full application state management. Applications created with Redux can easily be tested and can be run in dissimilar environments showing consistent behavior.

37. Explain the 3 principles that Redux follows?

  1. Single source of truth: The state of the whole application is deposited in an object or state tree within a store. The single state tree makes it convenient to keep track of changes over duration or while debugging or inspecting the application.
  2. The state is read-only: The only way to modify the state is to start an action. An action is a plain JavaScript object telling the change. Same as the state is the nominal representation of data; the action is the nominal representation of the change to the data.
  3. Changes are made with pure functions: To tell how the state tree is altered by actions, you require pure functions. Pure functions are those functions whose return value depends specifically on the values of their arguments.

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

Redux uses ‘Store’ for keeping the applications whole state in one place. So all the component’s state is deposited in the Store and they collect updates from the Store itself. The single state tree easily tracks the changes for some duration and debug or inspects the application.

39. Explain the components of Redux.

  1. Action: It’s an object that defines what happened.
  2. Reducer: It tells how the state will change.
  3. Store: State/ Object tree of the complete application is collected in the Store.
  4. View: Simply shows the data given by the Store.

40. Define Actions in Redux?

Actions in React do require a type property that shows the type of ACTION being performed. They must be described as a String constant in which you can add more properties. In Redux, actions are made using the functions called Action Creators. For Example:

function addTodo(text) {

return {

type: ADD_TODO,

text

}

}

You may also prepare:

React Advanced Interview Questions

41. What is the role of the Reducer?

Reducers are pure functions that tell how the application’s state is changing in reply to an ACTION. Reducers take you to the previous state and action and come with a new state. It defines what kind of update needs to be done depending on the type of the action, and their returning new values. , if no work is done then it returns the previous state as it is.

42. Tell the significance of the Store in Redux?

A store is a JS object which can sustain the application’s state and gives a few helper methods to access the state, dispatch actions, and register listeners. The complete state/ object tree of an application is stored in a single store which results in making Redux very simple and predictable. Middleware is passed to the store to manage the processing of data and to keep a log of several actions that modify the state of stores. All the actions bring back a new state via reducers.

43. Differentiate Redux and Flux?

Flux:

  1. The Store has both state and change logic
  2. Multiple stores are available
  3. All the stores are separated and flat
  4. Has singleton dispatcher
  5. React components subscribe to store
  6. The state is mutable

Redux:

  1. Store and change logic is treated separately
  2. The only single store is available
  3. Single store with hierarchical reducers
  4. No concept of a dispatcher
  5. Container components utilize connect
  6. The state is immutable

44. Tell some advantages of Redux?

  • Predictability of outcome: As there is only one source of truth, that is the store, so there is no misunderstanding about how to synchronize the current state with actions and different parts of the application.
  • Maintainability: It is easy to maintain with an expected outcome and strict structure.
  • Server-side rendering: You require passing the store made on the server, to the client-side. For initial render, it is very useful that provide a better user experience that enhances the application performance.
  • Developer tools: From actions to state modifications, developers can see everything in real-time of application ongoing.
  • Community and ecosystem: Redux has a vast community that makes it even more fascinating to use and build a healthy ecosystem.
  • Ease of testing – Redux’s code mostly contains functions that are pure, small, and isolated which results in making code testable and independent.
  • Organization – Redux is specific about how code should be organized; which results in making the code more consistent and easy when a team works with it.

45. Explain React Router?

It is a strong routing library built on top of React, which allows you to add new screens and flows to the application. This keeps the URL in synchronization with the data that is shown on the web page. It upholds a standardized architecture and behavior and is used for making single-page web applications. React Router has a simple API.

46. Why is the switch keyword used in React Router v4?

Although a
is used to capture multiple routes in the Router, the ‘switch’ keyword is used when you want to show only one route that needs to be rendered amongst the other defined routes. The tag matches the typed URL with the definite routes in serial order. When the first match is found, it renders the stated route, bypassing the remaining routes.

47. Explain the need for a Router in React?

It is used to define many routes and if a user writes a specific URL, and it matches the path of any ‘route’ defined inside the router, then the user is sent to that specific route. We require a Router library for our app that permits creating different routes with each route leading to us a unique view.

48. Tell the advantages of React Router.

  1. As React depends on components, In React Router v4 the API is ‘All about Components’. A Router can be pictured as a single root component () in which we enfold the particular child routes ().
  2. In React Router v4, we don’t require manually setting History values and all that needs to be done is to wrap our routes within the component.
  3. There are three packages for Web, Native, and Core. The compact size of our application is supported by these packages. It can easily be switched over to a similar coding style.

49. Differentiate between React Router and conventional routing?

  • Pages Involved: In conventional routing, each view corresponds to a new file, and in react routing only a single HTML page is involved.
  • URL Changes: In conventional routing, an HTTP request is sent to a server and a matching HTML page is received, and in react routing the History attribute is changed
  • Feel: In conventional routing, the user navigates across different pages for each view, and in react routing user is duped thinking he is navigating across different pages.

Want to prepare for these languages:

Recent Articles