React JS Certification Training Course
- 10k Enrolled Learners
- Weekend
- Live Class
Selecting the right technology for developing an application or website is becoming more challenging. Among all, React is considered as the fastest growing Javascript framework. Slowly and steadily, the JavaScript tools are firming their roots in the marketplace and the demand for React certification is increasing exponentially. With a quick learning curve, reusable components, and clean abstraction, React is a clear win for front-end developers across the globe. So, if you are an aspiring front-end developer and preparing for interviews, then this blog on Top 50 React Interview Questions is for you.
Let us start by taking a look at some of the most frequently asked React Interview Questions:
1. Differentiate between Real DOM and Virtual DOM.
2. What is React?
3. What are the features of React?
4. List some of the major advantages of React.
5. What are the limitations of React?
6. What is JSX?
7. What do you understand by Virtual DOM? Explain its working.
8. Why can’t browsers read JSX?
9. How different is React’s ES6 syntax when compared to ES5?
10. How is React different from Angular?
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 50 React Interview Questions and Answers which are most likely to be asked by the interviewer. For your ease of access, I have categorized the React interview questions namely:
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. |
Major features of React are listed below:
Some of the major advantages of React are:
Limitations of React are listed below:
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> ); }
A virtual DOM is a lightweight JavaScript object which originally is just a 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. Check out this Web developer course online to learn more about react.
This Virtual DOM works in three simple steps.
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.
Syntax has changed from ES5 to ES6 in the following aspects:
// ES5 var React = require('react'); // ES6 import React from 'react';
// ES5 module.exports = Component; // ES6 export default Component;
// ES5 var MyComponent = React.createClass({ render: function() { return <h3>Hello Edureka!</h3> ; } }); // ES6 class MyComponent extends React.Component { render() { return <h3>Hello Edureka!</h3> ; } }
// 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> ; } }
// 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> ; } }
TOPIC | REACT | 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 |
6. AUTHOR |
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.
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.
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') );
Props is the shorthand 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 throughout 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.
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().
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 |
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') );
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) } /> ); }
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. |
There are three different phases of React component’s lifecycle:
21. Explain the lifecycle methods of React components in detail.
Some of the most important lifecycle methods are:
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:
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.
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> ); } });
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.
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> ); } }
Following are the cases when refs should be used:
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> ); } }
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> ); }
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 |
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 wrap 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.
HOC can be used for many tasks like:
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.
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.
Following are some of the major problems with MVC framework:
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.
Redux is one of the most trending 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.
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.
Redux is composed of the following components:
In case you are facing any challenges with these React interview questions, please comment on your problems in the section below.
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 } }
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.
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 the 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.
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 |
In case you are facing any challenges with these React interview questions, please comment on your problems in the section below.
Advantages of Redux are listed below:
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.
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.
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>
Few advantages are:
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 a 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 |
I hope this set of React Interview Questions and Answers will help you in preparing for your interviews. All the best!
If you want to get trained in web development and wish to develop interesting UI’s on your own, then check out the Web Development Certification Training by Edureka, a trusted online learning company with a network of more than 250,000 satisfied learners spread across the globe.
Got a question for us? Please mention it in the comments section and we will get back to you.
Course Name | Date | |
---|---|---|
React JS Certification Training Course | Class Starts on 28th January,2023 28th January SAT&SUN (Weekend Batch) | View Details |
React JS Certification Training Course | Class Starts on 25th February,2023 25th February SAT&SUN (Weekend Batch) | View Details |
edureka.co
Amazing collection, so useful for React Programmers to find out their efficiency and improve knowledge with the help of above questions.
Thanks a lot for sharing it, Really appriciable.
thank you , this is really good list of questions
@Swatee Chand 21. Explain the lifecycle methods of React components in detail. in ShouldComponentUpdate() method default value is true instead of false.
please correct there.
Yes you are right. Thanks for correction. Same question is asked me during interview. Finally landed confirmation email. Thanks edureka.
some of these life cycles are outdated. and new ones are added.
Question 21: shouldComponentUpdate – returns true by default.
It’s very important to pay attention to this one of the best way to improve performance is to override shouldComponentUpdate.
See https://reactjs.org/docs/optimizing-performance.html
also read about React.PureComponent – component where the shouldComponentUpdate is done with shallow comparision
I have a problem with Question 3: What are the features of React?
This answer:
ii. It uses server-side rendering.
seems too specific for a very generic question. You don’t have to use server-side rendering with React. This Answer makes it sound like you will always use server-side rendering in every React project.
i totally agree with this! server-side rendering is something you very rarely do.
Also please have a review on this answer
4. What are the limitations of React?
Limitations of React are listed below:
React is just a library, not a full-blown framework – Correct
Its library is very large and takes time to understand – Well library is simple not very large as compare to Angular and Ember
It can be little difficult for the novice programmers to understand – its better then Ember and Angular ,Also React dcumentation is comprehensively structured
Coding gets complex as it uses inline templating and JSX – Coding standards can be implemented to have complexity divided to modularity
Very Nice article comprimising almost all React and Redux related questions please also add following questions
1) Render Prop Pattern
2) Fiber Reconciller
3) Whats better Redux vs Mobx vs Flow
4) Redux Sagas / Observables and What are Middlewares
5) Why is Immutability so Important