Change improper translation (#32829)

This commit is contained in:
Leo
2019-01-26 14:04:05 -05:00
committed by Jingyi Ding
parent fef2ed3c11
commit 20607de9ac

View File

@ -1,85 +1,90 @@
--- ---
title: State vs Props title: State vs Props
localeTitle: 州与道具 localeTitle: state 与 props
--- ---
## 州与道具 ## state 与 props
当我们开始使用React组件时我们经常听到两个术语。他们是`state``props` 。因此,在本文中,我们将探讨它们的含义以及它们之间的区别。 当我们开始使用React组件时我们经常听到两个术语。他们是`state``props` 。因此,在本文中,我们将探讨它们的含义以及它们之间的区别。
## ## State
* 状态是组件拥有的东西。它属于定义它的特定组件。 例如,一个人的年龄是该人的状态。 * state是类组件拥有的东西。它属于定义它的特定组件。 例如,一个人的年龄是该人的状态。
* 国家是可变的。但它只能由拥有它的组件来改变。因为我只能改变我的年龄,而不是其他任何人。 * state的值是可变的。但它只能由拥有它的组件来改变。因为我只能改变我的年龄,而不是其他任何人。
* 您可以使用`this.setState()`更改状态 * 您可以使用`this.setState()`更改组件的state值
请参阅以下示例以了解状态: 请参阅以下示例以了解状态:
#### Person.js #### Person.js
```javascript ```javascript
import React from 'react'; import React from 'react';
class Person extends React.Component{ // 此组件为类类型组件
constructor(props) { class Person extends React.Component{
super(props);
this.state = { // 初始化state
age:0 constructor(props) {
this.incrementAge = this.incrementAge.bind(this) super(props);
} this.state = {
age:0
incrementAge(){ this.incrementAge = this.incrementAge.bind(this)
this.setState({ }
age:this.state.age + 1;
}); // 修改state值
} incrementAge(){
this.setState({
render(){ age:this.state.age + 1;
return( });
<div> }
<label>My age is: {this.state.age}</label>
<button onClick={this.incrementAge}>Grow me older !!<button> render(){
</div> return(
); <div>
} <label>My age is: {this.state.age}</label> //利用this.state.XXX去获取 state
} <button onClick={this.incrementAge}>Grow me older !!<button>
</div>
export default Person; );
``` }
}
在上面的例子中, `age``Person`组件的状态。
export default Person;
## 道具: ```
* 道具类似于方法参数。它们被传递到使用该组件的组件 在上面的例子中, `age``Person`组件的state
* 道具是不可改变的。它们是只读的。
## Props
请参阅以下示例以了解道具:
* props类似于方法参数。它们被传递到使用该组件的组件。
#### Person.js * 道具是不可改变的。它们是只读的。
```javascript 请参阅以下示例以了解道具:
import React from 'react';
#### Person.js
class Person extends React.Component{
render(){ ```javascript
return( import React from 'react';
<div>
<label>I am a {this.props.character} person.</label> class Person extends React.Component{
</div> render(){
); return(
} <div>
} <label>I am a {this.props.character} person.</label>
</div>
export default Person; );
}
const person = <Person character = "good"></Person> }
```
export default Person;
在上面的例子中, `const person = <Person character = "good"></Person>`我们将`const person = <Person character = "good"></Person>` `character = "good"` prop传递给`Person`组件。
const person = <Person character = "good"></Person>
它输出为“我是一个好人”,事实上我是。 ```
关于州和道具还有很多东西需要学习。通过实际潜入编码可以学到很多东西。因此,通过编码让你的手弄脏 在上面的例子中, `const person = <Person character = "good"></Person>`我们将`const person = <Person character = "good"></Person>` `character = "good"` prop传递给`Person`组件。然后您可以在Person组件中通过 props.character 去调用上层传入的character值
如果需要,请在[推特](https://twitter.com/getifyJr)上与我联系 它输出为“我是一个好人”,事实上我也确实是
关于state和props我们还有很多东西需要学习。通过实际编程我们可以学到更多东西。因此快去实践吧。
如有需要,请在[推特](https://twitter.com/getifyJr)上与我联系。
编码快乐!!!