• +91 9723535972
  • info@interviewmaterial.com

ReactJS Interview Questions and Answers

Related Subjects

ReactJS Interview Questions and Answers

Question - 71 : - How are forms created in React?

Answer - 71 : -

Forms allow the users to interact with the application as well as gather information from the users. Forms can perform many tasks such as user authentication, adding user, searching, filtering, etc. A form can contain text fields, buttons, checkbox, radio button, etc.

React offers a stateful, reactive approach to build a form. The forms in React are similar to HTML forms. But in React, the state property of the component is only updated via setState(), and a JavaScript function handles their submission. This function has full access to the data which is entered by the user into a form.

import React, { Component } from 'react';  
  
class App extends React.Component {  
  constructor(props) {  
      super(props);  
      this.state = {value: ''};  
      this.handleChange = this.handleChange.bind(this);  
      this.handleSubmit = this.handleSubmit.bind(this);  
  }  
  handleChange(event) {  
      this.setState({value: event.target.value});  
  }  
  handleSubmit(event) {  
      alert('You have submitted the input successfully: ' + this.state.value);  
      event.preventDefault();  
  }  
  render() {  
      return (  
            
           

Controlled Form Example

  
           
                Name:  
                  
              
              
           
      );  
  }  
}  

Question - 72 : - What are the different phases of React component's lifecycle?

Answer - 72 : -

The different phases of React component's lifecycle are:

  • Initial Phase: It is the birth phase of the React lifecycle when the component starts its journey on a way to the DOM. In this phase, a component contains the default Props and initial State. These default properties are done in the constructor of a component.

  • Mounting Phase: In this phase, the instance of a component is created and added into the DOM.

  • Updating Phase: It is the next phase of the React lifecycle. In this phase, we get new Props and change State. This phase can potentially update and re-render only when a prop or state change occurs. The main aim of this phase is to ensure that the component is displaying the latest version of itself. This phase repeats again and again.

  • Unmounting Phase: It is the final phase of the React lifecycle, where the component instance is destroyed and unmounted(removed) from the DOM.

Question - 73 : - Explain the lifecycle methods of React components in detail.

Answer - 73 : -

The important React lifecycle methods are:

  • getInitialState(): It is used to specify the default value of this.state. It is executed before the creation of the component.
  • componentWillMount(): It is executed before a component gets rendered into the DOM.
  • componentDidMount(): It is executed when the component gets rendered and placed on the DOM. Now, you can do any DOM querying operations.
  • componentWillReceiveProps(): It is invoked when a component receives new props from the parent class and before another render is called. If you want to update the State in response to prop changes, you should compare this.props and nextProps to perform State transition by using this.setState() method.
  • shouldComponentUpdate(): It is invoked when a component decides any changes/updation to the DOM and returns true or false value based on certain conditions. If this method returns true, the component will update. Otherwise, the component will skip the updating.
  • componentWillUpdate(): It is invoked before rendering takes place in the DOM. Here, you can't change the component State by invoking this.setState() method. It will not be called, if shouldComponentUpdate() returns false.
  • componentDidUpdate(): It is invoked immediately after rendering takes place. In this method, you can put any code inside this which you want to execute once the updating occurs.
  • componentWillUnmount(): It is invoked immediately before a component is destroyed and unmounted permanently. It is used to clear up the memory spaces such as invalidating timers, event listener, canceling network requests, or cleaning up DOM elements. If a component instance is unmounted, you cannot mount it again.

Question - 74 : - What are Pure Components?

Answer - 74 : -

Pure components introduced in React 15.3 version. The React.Component and React.PureComponent differ in the shouldComponentUpdate() React lifecycle method. This method decides the re-rendering of the component by returning a boolean value (true or false). In React.Component, shouldComponentUpdate() method returns true by default. But in React.PureComponent, it compares the changes in state or props to re-render the component. The pure component enhances the simplicity of the code and performance of the application.

Question - 75 : - What are Higher Order Components(HOC)?

Answer - 75 : -

In React, Higher Order Component is an advanced technique for reusing component logic. It is a function that takes a component and returns a new component. In other words, it is a function which accepts another function as an argument. According to the official website, it is not the feature(part) in React API, but a pattern that emerges from React's compositional nature.

Question - 76 : - What can you do with HOC?

Answer - 76 : -

You can do many tasks with HOC, some of them are given below:

  • Code Reusability
  • Props manipulation
  • State manipulation
  • Render highjacking

Question - 77 : - Why is it necessary to start component names with a capital letter?

Answer - 77 : -

In React, it is necessary to start component names with a capital letter. If we start the component name with lower case, it will throw an error as an unrecognized tag. It is because, in JSX, lower case tag names are considered as HTML tags.

Question - 78 : - What are fragments?

Answer - 78 : -

In was introduced in React 16.2 version. In React, Fragments are used for components to return multiple elements. It allows you to group a list of multiple children without adding an extra node to the DOM.

Example

render() {  
  return (  
      
        
        
        
      
  )  
}  
There is also a shorthand syntax exists for declaring Fragments, but it's not supported in many tools:

render() {  
  return (  
    <>  
        
        
        
      
  )  
}  

Question - 79 : - Why are fragments better than container divs?

Answer - 79 : -

  • Fragments are faster and consume less memory because it did not create an extra DOM node.
  • Some CSS styling like CSS Grid and Flexbox have a special parent-child relationship and add
    tags in the middle, which makes it hard to keep the desired layout.
  • The DOM Inspector is less cluttered.

Question - 80 : - What is create-react-app?

Answer - 80 : -

Create React App is a tool introduced by Facebook to build React applications. It provides you to create single-page React applications. The create-react-app are preconfigured, which saves you from time-consuming setup and configuration like Webpack or Babel. You need to run a single command to start the React project, which is given below.

$ npx create-react-app my-app  
This command includes everything which we need to build a React app. Some of them are given below:

  • It includes React, JSX, ES6, and Flow syntax support.
  • It includes Autoprefixed CSS, so you don't need -webkit- or other prefixes.
  • It includes a fast, interactive unit test runner with built-in support for coverage reporting.
  • It includes a live development server that warns about common mistakes.
  • It includes a build script to bundle JS, CSS, and images for production, with hashes and source maps.


NCERT Solutions

 

Share your email for latest updates

Name:
Email:

Our partners