Use class-style component syntax
时间: 2023-07-25 21:03:40 浏览: 67
Sure! Here's an example of a class-style component in React:
```
import React, { Component } from 'react';
class MyComponent extends Component {
constructor(props) {
super(props);
this.state = {
count: 0
};
}
handleClick = () => {
this.setState(prevState => ({
count: prevState.count + 1
}));
}
render() {
return (
<div>
<p>Count: {this.state.count}</p>
<button onClick={this.handleClick}>Click me</button>
</div>
);
}
}
export default MyComponent;
```
In this example, `MyComponent` is defined as a class that extends `Component`. It has a constructor method that sets the initial state of the component, and a `handleClick` method that updates the state when the button is clicked. The `render` method returns the JSX that will be displayed on the page. Finally, the component is exported so it can be used in other parts of the application.