51 Most Important ReactJS Interview Questions and Answers
To conclude our 6 Articles Series on ReactJS, here we are providing 51 Most Important and Frequently Asked ReactJSInterview Questions. These questions will be very useful for you to crack an interview and help you to make career in ReactJS.
ReactJS Tutorial Index
- ReactJS Tutorial# 1 – An Introduction
- ReactJS Tutorial# 2 – Components and Props
- ReactJS Tutorial# 3 – Component State and Lifecycle
- ReactJS Tutorial# 4 – Forms, Events, and Keys
- ReactJS Tutorial# 5 – React Flux and Redux
- ReactJS Tutorial# 6 – ReactJS Best Practices
-
What is Reactjs :
ReactJS is a frontend Open source Javascript library initially developed by Facebook. React was used on facebook.com in 2011 and one year later it was used on instagram.com. In 2015, Facebook has announced react-native, another library that uses the same architecture to create native Android and iOS applications.
-
Mention five benefits of ReactJS :
Easy learning curve :
React is easy to learn. Anyone with a good understanding of HTML, CSS and Javascript can learn to react easily.
Readability :
React uses JSX that improves the readability of the code.
Performance :
React uses virtual DOM to update the UI that doesn’t require to update the whole webpage for every single change. That makes the application faster.
Reusable components :
We can use one component in many places without rewriting the code.
SSR :
React supports server-side rendering that improves the page load time.
-
What is Server side rendering? Does react support it?
React supports server-side rendering. Server-side rendering is the process of rendering a web page on the server side than in the browser. It makes the page load time faster. SSR improves the performance, SEO and user experience.
-
What is JSX?
JSX or JavaScript XML is used in React to include XML/HTML code in JavaScript code. Later, transpilers like babel can transform these code into proper javaScript code. We can use React without JSX but JSX makes it more elegant.
1 2 3 4 5 6 7 |
render(){ return( <div> <h1>Hello World</h1> </div> ); } |
-
What is virtual DOM in React?
Manipulating the real DOM makes one web application slow. React creates one virtual DOM for each real DOM object. The virtual DOM has the same properties as the real DOM and we can manipulate it faster than a real DOM. In react, all operations are performed on the virtual DOM object.
-
Can browser read JSX files?
The browser can’t read JSX code. JSX is not the actual JavaScript. Browsers are designed to read only JavaScript. So, a transformer like babel is used to convert JSX code to JavaScript objects.
-
What is a React component?
React components are the basic building block of a react application. These are like functions that take inputs and returns one react element based on the inputs. Using the components, we can split an application into reusable, independent pieces.
-
What are the types of React component?
React components can be categorized into two types :
- Function component
- Class component
-
Explain the function and class components.
Function component :
Function components are actually JavaScript functions. These functions return a react element based on input properties. For example :
1 2 3 |
function Header(props){ return <h1>Hi , {props.message}</h1> } |
Class components :
Class components are JavaScript ES6 class that extends ‘React.Component’. For example :
1 2 3 4 5 |
class Header extends React.Component { render() { return <h1>Hi, {this.props.message}</h1>; } } |
-
When to use a class component in React?
A class component is used if we have to deal with state or lifecycle methods. Function components are preferred otherwise.
-
What is React State?
React ‘state’ is an object that determines how a component renders and how it behaves. The state holds information that may change over the lifetime of a react component. The state is a private property of a component. Based on the value of a state, the react component updates its UI.
-
What are React props?
props are used to pass data to a component. They can hold single or multiple sets of values to pass to a component. They are mainly used to pass data to a component and to trigger any state changes.
-
Is there any difference between React state and props?
props and state both are used to hold data or information about a react component. ‘state’ objects are private and they are accessible only inside a component. ‘props’ are used to pass data from one component to another component.
-
How can we update state in react?
We can update the ‘state’ of a react component directly or indirectly. If we update it directly, it will not update the UI element linked to the state. The preferred way to update a ‘state’ is to use ‘setState’ method. This method updates a state object and re-renders the component automatically.
-
How does ‘setState’ function work?
‘setState’ function is used to update the state of a react component. If the UI of the component is dependent on the value we are updating, it will re-render the UI. For example :
1 |
setState({message : 'Hello'}) |
-
How to write a callback function in ‘setState’?
We can use one callback function in the ‘setState’ method that will invoke when the state is updated and the UI is re-rendered. For the callback, it is recommended to use React lifecycle methods rather than the callback method.
1 |
setState({message : 'Hello'}, () => console.log("Inside the callback method")) |
-
What are the main differences between ‘state’ and ‘props’?
- ‘props’ are used to pass different properties from the parent component to the child component. But ‘state’ is used inside a component to keep ‘data’ for that component.
- The values of ‘props’ can’t be changed in a child component but the value of ‘state’ can be changed.
- The UI of a component can’t be dependent on the ‘props’ but it can be dependent on the values of ‘state’.
- ‘props’ can’t update the UI but ‘state’ can re-render the UI automatically.
-
What is a stateless component?
A stateless component has no ‘state’. It may take one props as an argument and returns a react element based on the values of the ‘props’. These components also don’t have a lifecycle.
-
What is a stateful component?
‘stateful’ components are class components. They have one ‘state’ that is used for UI re-rendering.
-
List all react lifecycle methods.
Following are the lifecycle methods for React (16.3+) :
- getDerivedStateFromProps
- componentDidMount
- shouldComponentUpdate
- getSnapshotBeforeUpdate
- componentDidUpdate
- componentDidUpdate
-
How to write React comments?
We can write comments in react as like below :
1 2 3 4 5 6 7 8 9 10 |
<div> {/* This is a single line comment */} {/* This is a multiline comment */} </div> |
-
What is an event?
Events are triggered with respect to few specific user actions like a mouse click, keypress etc. Each event contains a set of different properties.
-
Show one example of an event.
1 2 3 4 5 6 7 8 9 10 11 |
function ClickHandler() { function onClicked(e) { console.log('Hello world is clicked.'); } return ( <a href="#" onClick={onClicked}> Hello world </a> ); } |
-
What is ‘SyntheticEvent’?
SyntheticEvent is a cross-browser wrapper around the browser’s native event. It is the same as the browser’s native event. It works identically across all browsers.
-
Can you use ‘addEventListener’ in react?
‘addEventListener’ is not recommended to use in ‘React’. Instead of adding a listener when the element is created, we should provide one listener during the initial rendering time of a DOM element.
-
What is ‘create-react-app’?
‘create-react-app’ is a command line tool to create one basic react application. It is officially supported by ‘Facebook’.
-
What is conditional rendering?
We can use if-else or conditional operator to render a react element based on a specific condition. This is called conditional rendering.
-
Set one example of conditional rendering.
1 2 3 4 5 6 7 8 9 |
function ShowWelcomeMsg(props) { const isFirstTimeLoggingIn = props.isFirstTime; if (isFirstTimeLoggingIn) { return <WelcomeComponent />; } return <WelcomeBackComponent />; } |
In this example, ‘WelcomeComponent’ will be shown if the value of ‘isFirstTimeLoggingIn’ is true, else ‘WelcomeBackComponent’ will be rendered.
-
How to use inline ‘if’ and ‘&&’ operator?
We can use one ‘if’ condition with ‘&&’ operator like below :
1 2 3 4 5 |
<div> { totalCount > 10 && <h1> Total count is greater than 10 </h1> } </div> |
Here, if the value of ‘totalCount’ is greater than ’10’, it will add the <h1></h1> line in the <div> block.
-
How to use inline ‘if-else’ and ‘&&’ operator in React?
1 2 3 4 5 6 7 |
<div> {totalCount > 10 ? ( <h1>Total count is greater than 10</h1> ) : ( <h1>Total count is less than 10</h1> ) </div> |
This will change the message in the <div> block based on the value of ‘totalCount’.
-
How to prevent a component from rendering in React?
To prevent a component from rendering, we can return ‘null’.
1 2 3 4 5 6 7 8 9 |
function ShowWelcomeMsg(props) { if (!props.isFirstTime) { return null; } return ( <div> Welcome to our website! </div> ); } |
Here, if the value of ‘isFirstTime’ is false, it will not render the component.
-
What are createElement and cloneElement?
React.createElement is used to create one react element which will be used as a UI component. React.cloneElement is used to clone an element.
-
Can we create one list without using ‘key’ in react?
We can create one list without ‘key’ for each element but it will cause issues with the react component rendering the list. Even on the browser console, it will throw one error.
-
What is the use of ‘key’ in react list?
Keys are used to provide each list element with a stable identity. The keys should be unique. The best practice is to use ‘id’ of the data as the key.
-
Is it a good practice to use item index as ‘key’ of list elements?
It is not a good practice to use the index of an item as the ‘key’ of the list elements. For example, if the order of the items changes, it will impact negatively and may cause issues with the react component state.
-
Is it required the keys to be unique globally in a react application?
The keys should be unique among the sibling lists. But we can use the same keys if we have two different arrays.
-
What is Lifting state up in React?
If several states need to share common data, we can lift the state up to its closest common ancestor. That means the child components will not hold any local state, instead, the parent component will hold the common state. This same state will be used by all parent and child components.
-
What are children prop?
Children props are used to pass components to other components as properties. This can be accessed by using {props.children}. For example :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
function ShowMessage(props) { return <div>{props.children}</div>; } class MainComponent extends React.Component { render() { return ( <ShowMessage> <div>Hello</div> <div>World</div> </ShowMessage> ); } } ReactDOM.render( <MainComponent />, document.getElementById("root") ); |
We are accessing the children inside ‘ShowMessage’ function.
-
What is the dynamic import in React?
Dynamic import is used to load a component dynamically. For example :
1 2 3 |
import("./ShowMessage").then(showMessage => { console.log(showMessage.show("Hello")); }); |
-
What is react Context?
React context is used to pass data through a tree of react components. Using context, we can share data globally between react components.
-
How can we create one context object?
We can create one context object using the ‘React.createContext({})’ function call. The argument of this function is the default value.
-
What are Context provider and Context consumer?
Context provider is used to provide values to a context. The provided values can be used by any other components in the application. Similarly, context consumers are used to reading the value of a context.
-
What are error boundaries?
Error boundaries are used to catch JavaScript errors anywhere in the child components. These are used to log the error and show a fallback UI. It was introduced in React 16.
-
What are Fragments?
Fragments are used to group a list of children components without using any extra nodes to the DOM. For example :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
render() { return ( <React.Fragment> <ComponentA /> <ComponentB /> <ComponentC /> </React.Fragment> ); } |
-
What are empty tags <></> used for ?
Empty tags are used for declaring fragments in React.
-
What are higher-Order components ?
A higher-order component is a function that takes one react component and returns one new component. We can use it for state abstraction/manipulation, props manipulation or for reusing of code.
-
What are react portals?
Portal is used to render children into a DOM node. ‘createPortal’ method is used for it.
1 |
ReactDOM.createPortal(child, container) |
The second argument is a DOM element and the first argument is any renderable react child.
-
What are the difference beween getInitialState() and constructor() ?
If we are creating one component by extending ‘React.Component’, the constructor is used to initialize the state. Again, if we are creating one component by using the ‘React.createClass’ function, ‘getInitialState’ is used to initialize the state.
-
What is StrictMode?
StrictMode is used to run additional checks and warnings for react components. It runs only on development build. It doesn’t impact the production build. It highlights the problems in an application without rendering any visible UI. ‘<React.StrictMode> </React.StrictMode>’ are used to enable strictmode for any part of an application.
-
What is ‘prop-types’ library used for?
‘prop-types’ library is used to do runtime type checking for props and similar objects in a react application. Using ‘npm’, we can install this library.
1 |
npm install --save prop-types |
-
What are react hooks?
React hooks are introduced in React 16.8. Using hooks, we can use state and other react features without writing a class.
1 2 3 4 5 6 7 8 9 10 11 |
import React, { useState } from "react"; function Clicker() { const [click, setClick] = useState(0); return ( <div> <button onClick={() => setClick(click + 1)}>Click me</button> </div> ); } |
In this example, we are declaring one state variable ‘click’ with the initial state ‘0’. ‘useState’ is a hook in this example.
Hope you find these questions and answers useful. Please let us know if need to know more or any question for us. Happy to answer you.
All the best !