adspace
Answer Posted / Meenakshi Kumari
To update the state of a component in React, use the `setState()` method provided by React. The general approach is to define a new state object with the desired changes and pass it as an argument to `setState()`. Here's an example:
```
class MyComponent extends React.Component {
constructor(props) {
super(props);
this.state = { count: 0 };
}
handleClick() {
this.setState({ count: this.state.count + 1 });
}
render() {
return (
<div>
<button onClick={this.handleClick}>Click me</button>
<p>The count is {this.state.count}</p>
</div>
);
}
}
```
| Is This Answer Correct ? | 0 Yes | 0 No |
Post New Answer View All Answers