In this article, you will learn ReactJS Interview Questions and Answers Intermediate Level that are most frequently asked in interviews. Before proceeding to learn ReactJS Interview Questions and Answers – Intermediate Level, first learn the complete ReactJS Tutorial, and ReactJS Interview Questions and Answers – Beginner Level.
ReactJS is an open-source JavaScript library that is used for building user interfaces in a declarative and efficient way. It is a component-based front-end library responsible only for the view layer of an MVC (Model View Controller) architecture. React is used to create modular user interfaces and it promotes the development of reusable UI components that display dynamic data.
Pre-requisite: ReactJS Interview Questions and Answers (2023) – Beginner Level
This set contains the intermediate-level questions asked in the interview.
1. What is conditional rendering in React?
When there are multiple components in react and we want to render components according to our preference and some conditions then we use conditional rendering. In conditional rendering, a condition is specified and if the condition is passed then this component is rendered.
Let us look at this sample code to understand conditional rendering.
{isLoggedIn == false ? <DisplayLoggedOut /> : <DisplayLoggedIn />}
Here if the boolean isLoggedIn is false then DisplayLoggedOut component will be rendered otherwise DisplayLoggedIn component will be rendered.
2. What is react router?
React Router is a standard library for routing in React. It enables the navigation among views of various components in a React Application, allows changing the browser URL, and keeps the UI in sync with the URL.
To install react router type the following command.
npm i react-router-dom
3. Explain the components of a react-router
The main components of a react-router are:
- Router(usually imported as BrowserRouter): It is the parent component that is used to store all of the other components. Everything within this will be part of the routing functionality
- Switch: The switch component is used to render only the first route that matches the location rather than rendering all matching routes.
- Route: This component checks the current URL and displays the component associated with that exact path. All routes are placed within the switch components.
- Link: The Link component is used to create links to different routes.
4. Explain the lifecycle methods of components
A React Component can go through four stages of its life as follows.
- Initialization: This is the stage where the component is constructed with the given Props and default state. This is done in the constructor of a Component Class.
- Mounting: Mounting is the stage of rendering the JSX returned by the render method itself.
- Updating: Updating is the stage when the state of a component is updated and the application is repainted.
- Unmounting: As the name suggests Unmounting is the final step of the component lifecycle where the component is removed from the page.
5. Explain the methods used in mounting phase of components
Mounting is the phase of the component lifecycle when the initialization of the component is completed and the component is mounted on the DOM and rendered for the first time on the webpage. he mounting phase consists of two such predefined functions as described below
- componentWillMount() Function: This function is invoked right before the component is mounted on the DOM.
- componentDidMount() Function: This function is invoked right after the component is mounted on the DOM.
6. What is this.setState function in React?
We use the setState() method to change the state object. It ensures that the component has been updated and calls for re-rendering of the component. The state object of a component may contain multiple attributes and React allows using setState() function to update only a subset of those attributes as well as using multiple setState() methods to update each attribute value independently.
7. What is the use of ref in React?
Refs are a function provided by React to access the DOM element and the React element that you might have created on your own. They are used in cases where we want to change the value of a child component, without making use of props and all. They have wide functionality as we can use callbacks with them.
The syntax to use ref is
const node = this.myCallRef.current;
8. What are hooks in React?
Hooks are a new addition in React 16.8. They let developers use state and other React features without writing a class. Hooks doesn’t violate any existing React concepts. Instead, Hooks provide a direct API to react concepts such as props, state, context, refs and life-cycle
9. Explain the useState hook in React?
The most used hook in React is the useState() hook. It allows functional components to manipulate DOM elements before each render. Using this hook we can declare a state variable inside a function but only one state variable can be declared using a single useState() hook. Whenever the useState() hook is used, the value of the state variable is changed and the new variable is stored in a new cell in the stack.
We have to import this hook in React using the following syntax
import {useState} from 'react'
10. Explain the useEffect hook in react?
The useEffect hook in React eliminates the side effect of using class based components. It is used as an alternative to componentDidUpdate() method. The useEffect hook accepts two arguments where second argument is optional.
useEffect(function, dependency)
The dependency decides when the component will be updated again after rendering.
11. What is React Fragments?
when we are trying to render more than one root element we have to put the entire content inside the ‘div’ tag which is not loved by many developers. So since React 16.2 version, Fragments were introduced, and we use them instead of the extraneous ‘div’ tag. The following syntax is used to create fragment in react.
<React.Fragment>
<h2>Child-1</h2>
<p> Child-2</p>
</React.Fragment>
12. What is a react developer tool?
React Developer Tools is a Chrome DevTools extension for the React JavaScript library. A very useful tool, if you are working on React.js applications. This extension adds React debugging tools to the Chrome Developer Tools. It helps you to inspect and edit the React component tree that builds the page, and for each component, one can check the props, the state, hooks, etc.
13. How to use styles in ReactJS?
CSS modules are a way to locally scope the content of your CSS file. We can create a CSS module file by naming our CSS file as App.modules.css and then it can be imported inside App.js file using the special syntax mentioned below.
Syntax:
import styles from './App.module.css';
14. Explain styled components in React?
Styled-component Module allows us to write CSS within JavaScript in a very modular and reusable way in React. Instead of having one global CSS file for a React project, we can use styled-component for enhancing the developer experience. It also removes the mapping between components and styles – using components as a low-level styling construct
The command to install styled components is
npm i styled-components
Using the below code we can custom style a button in React
Javascript
import styled from 'styled-components' const Button = styled.div` width : 100px ; cursor: pointer ; text-decoration : none; ` export default Button; |
15. What is prop drilling and its disadvantages?
Prop drilling is basically a situation when the same data is being sent at almost every level due to requirements in the final level. The problem with Prop Drilling is that whenever data from the Parent component will be needed, it would have to come from each level, Regardless of the fact that it is not needed there and simply needed in last.
16. What are controlled and uncontrolled components in react?
A controlled component is a component which is managed by its parent component and its value is updated using props whereas uncontrolled components maintain their own state and data flow is done inside the component only unlike controlled components which pass data from parent to child
17. What is useRef hook in react?
The useRef is a hook that allows to directly create a reference to the DOM element in the functional component. The useRef returns a mutable ref object. This object has a property called .current. The value is persisted in the refContainer.current property. These values are accessed from the current property of the returned object.
Syntax:
const refContainer = useRef(initialValue);
18. Explain the componentDidMount method in React?
The componentDidMount() method allows us to execute the React code when the component is already placed in the DOM (Document Object Model). This method is called during the Mounting phase of the React Life-cycle i.e. after the component is rendered.
19. Difference between ES6 and ES5 syntax in React
The difference in different syntax is shown in the below table
Syntax | ES5 | ES6 |
importing | var React = require(‘react’); | import React from ‘react’; |
exporting | module.exports = Component; | export default Component; |
function definition | var sum = function(x, y) { return x + y; }; |
var sum = (x,y)=>{ return x+y }; |
20. What are synthetic event in React?
In order to work as a cross-browser application, React has created a wrapper same as the native browser in order to avoid creating multiple implementations for multiple methods for multiple browsers, creating common names for all events across browsers. Another benefit is that it increases the performance of the application as React reuses the event object.