Answers for "state creation and updation in class components reactjs interview questions"

0

state creation and updation in class components reactjs interview questions

class Car extends React.Component {
 constructor(props) {
   super(props);
   this.state = {
     brand: "BMW",
     color: "Black"
   };
 }

 changeColor() {
   this.setState(prevState => {
     return { color: "Red" };
   });
 }

 render() {
   return (
     <div>
       <button onClick={() => this.changeColor()}>Change Color</button>
       <p>{this.state.color}</p>
     </div>
   );
 }
}
Posted by: Guest on May-21-2021
0

state creation in class components reactjs interview questions

class Car extends React.Component{
 constructor(props){
   super(props);
   this.state = {
     brand: "BMW",
     color: "black"
   }
 }
}
Posted by: Guest on May-21-2021

Code answers related to "state creation and updation in class components reactjs interview questions"

Code answers related to "Javascript"

Browse Popular Code Answers by Language