Top 10 Most Asked React Interview Questions and Answers

Top 10 Most Asked React Interview Questions and Answers

Core React

Question no 1: What is React?

React is an open-source front-end JavaScript library that is used for building user interfaces, especially for single-page applications. It is used for handling the view layer for web and mobile apps. React was created by Jordan Walke, a software engineer working for Facebook. React was first deployed on Facebook’s News Feed in 2011 and on Instagram in 2012.

Question no 2: What are the major features of React?

The major features of React are:

  • It uses VirtualDOM instead of RealDOM considering that RealDOM manipulations are expensive.
  • Supports server-side rendering.
  • Follows Unidirectional data flow or data binding.
  • Uses reusable/composable UI components to develop the view.

Question no 3: What is JSX?

  1. JSX is a XML-like syntax extension to ECMAScript (the acronym stands for JavaScript XML). Basically, it just provides syntactic sugar for the React.createElement() function, giving us expressiveness of JavaScript along with HTML-like template syntax.
  2. In the example below text inside <h1> the tag is returned as a JavaScript function to the render function.
jsx harmony class App extends React.Component { 
render() {
<h1>{'Welcome to React world!'}</h1>

Question no 4: What is the difference between Element and Component?

An Element is a plain object describing what you want to appear on the screen in terms of the DOM nodes or other components. Elements can contain other Elements in their props. Creating a React element is cheap. Once an element is created, it is never mutated.

The object representation of React Element would be as follows:

const element = React.createElement( 'div', {id: 'login-btn'}, 'Login' )

The above React.createElement() the function returns an object:

{ type: 'div', props: { children: 'Login', id: 'login-btn' } }

And finally, it renders to the DOM using ReactDOM.render():

<div id='login-btn'>Login</div>

Whereas a component can be declared in several different ways. It can be a class with a render() method or it can be defined as a function. In either case, it takes props as an input, and returns a JSX tree as the output:

const Button = ({ onLogin }) => <div id={'login-btn'} onClick={onLogin}>Login</div>

Then JSX gets transpiled to a React.createElement() function tree:

const Button = ({ onLogin }) => React.createElement( 'div', { id: 'login-btn', onClick: onLogin }, 'Login' )

Question no 5: How to create components in React?

There are two possible ways to create a component.

Function Components: This is the simplest way to create a component. Those are pure JavaScript functions that accept props object as the first parameter and return React elements:

jsx harmony  function Greeting({ message }) {

return <h1>{Hello, ${message}`}</h1>}

Class Components: You can also use ES6 class to define a component. The above function component can be written as:

jsx harmony class Greeting extends React.Component { render() {return <h1>{`Hello, ${this.props.message}`}</h1>} }

Question no 6: When to use a Class Component over a Function Component?

If the component needs state or lifecycle methods then use the class component otherwise use the function component. However, from React 16.8 with the addition of Hooks, you could use state, lifecycle methods, and other features that were only available in the class component right in your function component. So, it is always recommended to use Function components, unless you need a React functionality whose Function component equivalent is not present yet, like Error Boundaries

Question no 7: What are Pure Components?

React.PureComponent is exactly the same as React.Component except that it handles the shouldComponentUpdate() method for you. When props or state changes, PureComponent will do a shallow comparison on both props and state. Component on the other hand won't compare current props and states to the next out of the box. Thus, the component will re-render by default whenever shouldComponentUpdate is called.

Question no 8: What is a state in React?

The state of a component is an object that holds some information that may change over the lifetime of the component. We should always try to make our state as simple as possible and minimize the number of stateful components.

  1. Let’s create a user component with a message state,
jsx harmony
class User extends React.Component {
constructor(props) {
this.state = {
message: 'Welcome to React world'
render() {
return (
![state](images/state.jpg)State is similar to props, but it is private and fully controlled by the component ,i.e., it is not accessible to any other component till the owner component decides to pass it.

Question no 9: What are props in React?

Props are inputs to components. They are single values or objects containing a set of values that are passed to components on creation using a naming convention similar to HTML-tag attributes. They are data passed down from a parent component to a child component.

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

Pass custom data to your component.

Trigger state changes.

Use via this.props.reactProp inside component's render() method.

For example, let us create an element with reactProp property:

jsx harmony <Element reactProp={‘1’} />This `reactProp` (or whatever you came up with) name then becomes a property attached to React's native props object which originally already exists on all components created using React library.props.reactProp

Question no 10: What is the difference between state and props?

Both props and states are plain JavaScript objects. While both of them hold information that influences the output of render, they are different in their functionality with respect to components. Props get passed to the component similar to function parameters whereas the state is managed within the component similar to variables declared within a function.



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Muhammad Umair

Muhammad Umair


MERN Stack Developer | Software Engineer| Frontend & Backend Developer | Javascript, React JS, Express JS, Node JS, MongoDB, SQL, and Python