If you are an aspiring front end developer and preparing for interviews, then this blog is for you. This blog on Top 30 React Interview Questions is the perfect guide for you to learn all the concepts required to clear a React interview. But before starting with the React Interview Questions, let’s take a quick look at React’s demand and status in the market.
As of today, there are about 1,000 contributors on Github. Unique features like Virtual DOM and reusable components grab the attention of front end developers. Despite being just a library for ‘View’ in MVC (Model-View-Controller), it’s giving strong competition to full-blown frameworks like Angular, Meteor, Vue, etc. Check out the belo2w graph which shows the trend of popular JS frameworks:
So, here are the Top 30 React Interview Questions which are most likely to be asked by the interviewer.
Q1. How React works? How Virtual-DOM works in React?
React creates a virtual DOM. When state changes in a component it firstly runs a “diffing” algorithm, which identifies what has changed in the virtual DOM. The second step is reconciliation, where it updates the DOM with the results of diff.
The HTML DOM is always tree-structured — which is allowed by the structure of HTML document. The DOM trees are huge nowadays because of large apps. Since we are more and more pushed towards dynamic web apps (Single Page Applications — SPAs), we need to modify the DOM tree incessantly and a lot. And this is a real performance and development pain.
The Virtual DOM is an abstraction of the HTML DOM. It is lightweight and detached from the browser-specific implementation details. It is not invented by React but it uses it and provides it for free.
ReactElements lives in the virtual DOM. They make the basic nodes here. Once we defined the elements,
ReactElements can be render into the “real” DOM.
ReactComponent is changing the state, diff algorithm in React runs and identifies what has changed. And then it updates the DOM with the results of diff. The point is – it’s done faster than it would be in the regular DOM.
|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.|
Q2. What is React?
- 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.
Q3. What are the features of React?
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.
Q4. What is JSX?
For example, below is the syntax for a basic element in React with JSX and its equivalent without it.
Equivalent of the above using
Q5. List some of the major advantages of React.
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
Q6. What are the limitations of React?
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
Q7. What do you understand by Virtual DOM? Explain its working.
This Virtual DOM works in three simple steps.
- Whenever any underlying data changes, the entire UI is re-rendered in Virtual DOM representation.
- Then the difference between the previous DOM representation and the new one is calculated.
- Once the calculations are done, the real DOM will be updated with only the things that have actually changed.
Q8. Why can’t browsers read JSX?
Q9. How is React different from Angular?
|1. ARCHITECTURE||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||Runtime debugging|
Q10. What do you understand from “In React, everything is a component.”
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.
Q.11 What are the differences between a class component and functional component?
Class components allows us to use additional features such as local state and lifecycle hooks. Also, to enable our component to have direct access to our store and thus holds state.
When our component just receives props and renders them to the page, this is a ‘stateless component’, for which a pure function can be used. These are also called dumb components or presentational components.
From the previous question, we can say that our
Booklist component is functional components and are stateless.
On the other hand, the
BookListContainer component is a class component.
Q12. Explain the purpose of render() in React.
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.
Q13. What is the difference between state and props?
The state is a data structure that starts with a default value when a Component mounts. It may be mutated across time, mostly as a result of user events.
Props (short for properties) are a Component’s configuration. Props are how components talk to each other. They are received from above component and immutable as far as the Component receiving them is concerned. A Component cannot change its props, but it is responsible for putting together the props of its child Components. Props do not have to just be data — callback functions may be passed in as props.
There is also the case that we can have default props so that props are set even if a parent component doesn’t pass props down.
Props and State do similar things but are used in different ways. The majority of our components will probably be stateless. Props are used to pass data from parent to child or by the component itself. They are immutable and thus will not be changed. State is used for mutable data, or data that will change. This is particularly useful for user input.
Q14. Differentiate between states and 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|
Q15. What are controlled components?
In HTML, form elements such as
<select> typically maintain their own state and update it based on user input. When a user submits a form the values from the aforementioned elements are sent with the form. With React it works differently. The component containing the form will keep track of the value of the input in it’s state and will re-render the component each time the callback function e.g.
onChange is fired as the state will be updated. A form element whose value is controlled by React in this way is called a “controlled component”.
With a controlled component, every state mutation will have an associated handler function. This makes it straightforward to modify or validate user input.
Q16. What is a higher order component?
A higher-order component (HOC) is an advanced technique in React for reusing component logic. HOCs are not part of the React API. They are a pattern that emerges from React’s compositional nature.
A higher-order component is a function that takes a component and returns a new component.
HOC’s allow you to reuse code, logic and bootstrap abstraction. HOCs are common in third-party React libraries. The most common is probably Redux’s connect function. Beyond simply sharing utility libraries and simple composition, HOCs are the best way to share behavior between React Components. If you find yourself writing a lot of code in different places that does the same thing, you may be able to refactor that code into a reusable HOC.
Q17. What is arrow function in React? How is it used?
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.
Q18. Differentiate between stateful and stateless components.
|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.|
Q19. What are the different phases of React component’s lifecycle?
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.
Q.20 What is
create-react-app is the official CLI (Command Line Interface) for React to create React apps with no build configuration.
We don’t need to install or configure tools like Webpack or Babel. They are preconfigured and hidden so that we can focus on the code. We can install easily just like any other node modules. Then it is just one command to start the React project.
It includes everything we need to build a React app:
- React, JSX, ES6, and Flow syntax support.
- Language extras beyond ES6 like the object spread operator.
- Autoprefixed CSS, so you don’t need
-webkit-or other prefixes.
- A fast interactive unit test runner with built-in support for coverage reporting.
- A live development server that warns about common mistakes.
- A build script to bundle JS, CSS, and images for production, with hashes and sourcemaps.
Q.21 What is Redux?
Redux is based on the idea that there should be only a single source of truth for your application state, be it UI state like which tab is active or Data state like the user profile details.
All of these data is retained by redux in a closure that redux calls a store . It also provides us a recipe of creating the said store, namely
createStore function accepts another function,
x as an argument. The passed in function is responsible for returning the state of the application at that point in time, which is then persisted in the store. This passed in function is known as the
This is a valid example reducer function:
This store can only be updated by dispatching an action. Our App dispatches an
action, it is passed into
reducer; the reducer returns a fresh instance of the
state; the store notifies our App and it can begin it’s re render as required.
Q.22 What is Redux Thunk used for?
Redux thunk is middleware that allows us to write action creators that return a function instead of an action. The thunk can then be used to delay the dispatch of an action if a certain condition is met. This allows us to handle the asyncronous dispatching of actions. The inner function receives the store methods
getState as parameters.
To enable Redux Thunk, we need to use
applyMiddleware() as below
Q.22 What is
PureComponent? When to use
PureComponent is exactly the same as Component except that it handles the
shouldComponentUpdate method for us. When
PureComponent will do a shallow comparison on both
Component on the other hand won’t compare current props and state to next out of the box. Thus, the component will re-render by default whenever
shouldComponentUpdate is called.
When comparing previous
It is good to prefer
Component whenever we never mutate our objects.
Q23. Explain the lifecycle methods of React components in detail.
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.
Q24. What is an event in React?
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.
Q25. What are synthetic events in React?
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.
Q26. What do you understand by refs in React?
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.
Q27. List some of the cases when you should use Refs.
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
Q28. What is
render() in React? And explain its purpose?
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
<div> etc. This function must be kept pure i.e., it must return the same result each time it is invoked.
Q29. What do you know about controlled and uncontrolled components?
|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|
Q.30 What is the difference between React Native and React?
I hope this set of React Interview Questions and Answers will help you in preparing for your interviews. All the best!
Got a question for us? Please mention it in the comments section and we will get back to you.