• +91 9723535972
  • info@interviewmaterial.com

ReactJS Interview Questions and Answers

Related Subjects

Question - How can you update the State of a component?

Answer -

We can update the State of a component using this.setState() method. This method does not always replace the State immediately. Instead, it only adds changes to the original State. It is a primary method which is used to update the user interface(UI) in response to event handlers and server responses.

Example

import React, { Component } from 'react';  
import PropTypes from 'prop-types';  
  
class App extends React.Component {  
   constructor() {  
      super();        
      this.state = {  
          msg: "Welcome to JavaTpoint"  
      };      
      this.updateSetState = this.updateSetState.bind(this);  
   }  
   updateSetState() {  
       this.setState({  
          msg:"Its a best ReactJS tutorial"  
       });  
   }  
   render() {  
      return (  
         
  
             

{this.state.msg}

  
               
         
  
      );  
   }  
}

Comment(S)

Show all Coment

Leave a Comment




NCERT Solutions

 

Share your email for latest updates

Name:
Email:

Our partners