22.
Find the bug in this React code:
import React from 'react';
class App extends React.Component {
constructor(props) {
super(props);
this.state = {
name: 'John',
age: 25,
gender: 'male'
};
}
render() {
return (
<div>
<h1>Hello, {this.state.name}!</h1>
<p>You are {this.age}</p>
<p>Your gender is {this.state.gender}</p>
<div>
);
}
}
export default App;
The variable this.age should be this.state.age.
The gender key should be in quotes ('gender').
Missing closing tag for the div.
Missing render() method inside the class.
Correct: A, C
In React, to access the values stored in the component's state, we use this.state.variableName. Therefore, the correct way to access the age state variable is this.state.age. Additionally, there is a missing closing tag </div> in the JSX code.