freeCodeCamp/guide/chinese/certifications/front-end-libraries/react/create-a-stateful-component/index.md

49 lines
874 B
Markdown
Raw Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

---
title: Create a Stateful Component
localeTitle: 创建一个有状态组件
---
## 创建一个有状态组件
#### 提示1
```JSX
class StatefulComponent extends React.Component {
constructor(props) {
super(props);
// initialize state here
// "This" area may be a good place to use "dot" notation.
// dont forget to describe "name" property inside the state and assign your name to a property of "name".
}
render() {
return (
<div>
<h1>{this.state.name}</h1>
</div>
);
}
};
```
## 解
```JSX
class StatefulComponent extends React.Component {
constructor(props) {
super(props);
// initialize state here
this.state = {
name : "Name"
}
}
render() {
return (
<div>
<h1>{this.state.name}</h1>
</div>
);
}
};
```