freeCodeCamp/guide/chinese/react/component/index.md

79 lines
1.7 KiB
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: React - Components
localeTitle: React - 组件
---
## React - 组件
组件的意义是可在react中重用。您可以将值注入组件从而动态的展示不同的内容。如下所示
```jsx
function Welcome(props) {
return <h1>Hello, {props.name}</h1>;
}
const element = <Welcome name="Faisal Arkan" />;
ReactDOM.render(
element,
document.getElementById('root')
);
```
`name="Faisal Arkan"`将从`function Welcome(props)`赋予`{props.name}`值,并返回已通过`name="Faisal Arkan"`赋予值的组件之后React会将此组件渲染为html。
### 声明组件的其他方法
使用React.js时有许多方法可以声明组件但有两种组件 **_无状态_**组件和**_有状态_**组件。
### 有状态
#### 类类型组件
* 此组件为一个class类继承React的Component类。通过继承类类型组件拥有state变量此变量可以存储这个组件当前的状态。
* 类类型组件同时拥有state和props。
```jsx
class Cat extends React.Component {
constructor(props) {
super(props);
this.state = {
humor: 'happy'
}
}
render() {
return(
<div>
<h1>{this.props.name}</h1>
<p>
{this.props.color}
</p>
</div>
);
}
}
```
### 无状态组件
#### 功能组件ES6的箭头功能
* 无状态组件只是一般的javascript函数。因为没有继承任何类所以只能使用传入的props参数来注入值。
```jsx
const Cat = props => {
return (
<div>
<h1>{props.name}</h1>
<p>{props.color}</p>
</div>;
);
};
```
#### 隐式返回组件
```jsx
const Cat = props =>
<div>
<h1>{props.name}</h1>
<p>{props.color}</p>
</div>;
```