showing results for - "state creation in class components reactjs interview questions"
Jean
11 Jan 2020
1class Car extends React.Component{
2 constructor(props){
3   super(props);
4   this.state = {
5     brand: "BMW",
6     color: "black"
7   }
8 }
9}
10
Elena
28 Apr 2018
1class Car extends React.Component {
2 constructor(props) {
3   super(props);
4   this.state = {
5     brand: "BMW",
6     color: "Black"
7   };
8 }
9
10 changeColor() {
11   this.setState(prevState => {
12     return { color: "Red" };
13   });
14 }
15
16 render() {
17   return (
18     <div>
19       <button onClick={() => this.changeColor()}>Change Color</button>
20       <p>{this.state.color}</p>
21     </div>
22   );
23 }
24}
25