2018-10-10 18:03:03 -04:00
|
|
|
|
---
|
|
|
|
|
id: 5a24c314108439a4d4036171
|
2020-12-16 00:37:30 -07:00
|
|
|
|
title: 在用户界面中渲染状态
|
2018-10-10 18:03:03 -04:00
|
|
|
|
challengeType: 6
|
2020-09-18 00:13:42 +08:00
|
|
|
|
forumTopicId: 301409
|
2021-01-13 03:31:00 +01:00
|
|
|
|
dashedName: render-state-in-the-user-interface
|
2018-10-10 18:03:03 -04:00
|
|
|
|
---
|
|
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
|
# --description--
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
|
一旦定义了组件的初始 state,你就可以在要渲染的 UI 中显示它的任何部分。如果组件是有状态的,它将始终可以访问`render()`方法中`state`的数据。你就可以使用`this.state`访问数据。
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
|
如果你想在 render 方法的`return`中访问 state 值,你必须把这个值用花括号括起来。
|
|
|
|
|
|
|
|
|
|
`state`是 React 组件中最强大的特性之一,它允许你跟踪应用程序中的重要数据,并根据数据的变化渲染 UI。如果你的数据发生变化,你的 UI 也会随之改变。React 使用所谓的虚拟 DOM 来跟踪幕后的变化。当 state 数据更新时,它会使用该数据触发组件的重新渲染--包括接收 prop 数据的子组件。React 只在必要的时候更新实际的DOM,这意味着你不必担心 DOM 的变更,只需声明 UI 的外观即可。
|
|
|
|
|
|
|
|
|
|
注意,如果组件有状态,则没有其他组件知道它的`state`。它的`state`是完全封装的,或者是局限于组件本身的,除非你将 state 数据作为`props`传递给子组件。封装`state`的概念非常重要,因为它允许你编写特定的逻辑,然后将该逻辑包含并隔离在代码中的某个位置。
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
|
# --instructions--
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
|
在代码编辑器中,`MyComponent`是一个有状态组件,在组件的 render 方法中定义一个`h1`标签,该方法从组件的 state 渲染`name`的值。
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
|
**注意:** `h1`应该只渲染来自`state`的值。在 JSX 中,使用花括号`{ }`编写的任何代码都将被视为 JavaScript。因此,要访问`state`中的值,只需将引用括在花括号中即可。
|
|
|
|
|
|
|
|
|
|
# --hints--
|
|
|
|
|
|
|
|
|
|
`MyComponent`应该有一个键`name`,其值`freeCodeCamp`存储在其 state 中。
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
|
|
|
|
```js
|
2020-12-16 00:37:30 -07:00
|
|
|
|
assert(
|
|
|
|
|
Enzyme.mount(React.createElement(MyComponent)).state('name') ===
|
|
|
|
|
'freeCodeCamp'
|
|
|
|
|
);
|
2018-10-10 18:03:03 -04:00
|
|
|
|
```
|
|
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
|
`MyComponent`应该在`div`中渲染一个`h1`标题。
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
|
```js
|
|
|
|
|
assert(
|
|
|
|
|
/<div><h1>.*<\/h1><\/div>/.test(
|
|
|
|
|
Enzyme.mount(React.createElement(MyComponent)).html()
|
|
|
|
|
)
|
|
|
|
|
);
|
|
|
|
|
```
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
|
渲染的`h1`标题中应该包含一段文本,这段文本是从组件的 state 中渲染出来的。
|
2020-09-18 00:13:42 +08:00
|
|
|
|
|
2018-10-10 18:03:03 -04:00
|
|
|
|
```js
|
2020-12-16 00:37:30 -07:00
|
|
|
|
async () => {
|
|
|
|
|
const waitForIt = (fn) =>
|
|
|
|
|
new Promise((resolve, reject) => setTimeout(() => resolve(fn()), 250));
|
|
|
|
|
const mockedComponent = Enzyme.mount(React.createElement(MyComponent));
|
|
|
|
|
const first = () => {
|
|
|
|
|
mockedComponent.setState({ name: 'TestName' });
|
|
|
|
|
return waitForIt(() => mockedComponent.html());
|
|
|
|
|
};
|
|
|
|
|
const firstValue = await first();
|
|
|
|
|
assert(firstValue === '<div><h1>TestName</h1></div>');
|
2020-09-18 00:13:42 +08:00
|
|
|
|
};
|
2018-10-10 18:03:03 -04:00
|
|
|
|
```
|
2020-08-13 17:24:35 +02:00
|
|
|
|
|
2021-01-13 03:31:00 +01:00
|
|
|
|
# --seed--
|
|
|
|
|
|
|
|
|
|
## --after-user-code--
|
|
|
|
|
|
|
|
|
|
```jsx
|
|
|
|
|
ReactDOM.render(<MyComponent />, document.getElementById('root'))
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
## --seed-contents--
|
|
|
|
|
|
|
|
|
|
```jsx
|
|
|
|
|
class MyComponent extends React.Component {
|
|
|
|
|
constructor(props) {
|
|
|
|
|
super(props);
|
|
|
|
|
this.state = {
|
|
|
|
|
name: 'freeCodeCamp'
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
render() {
|
|
|
|
|
return (
|
|
|
|
|
<div>
|
|
|
|
|
{ /* Change code below this line */ }
|
|
|
|
|
|
|
|
|
|
{ /* Change code above this line */ }
|
|
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
```
|
|
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
|
# --solutions--
|
|
|
|
|
|
2021-01-13 03:31:00 +01:00
|
|
|
|
```jsx
|
|
|
|
|
class MyComponent extends React.Component {
|
|
|
|
|
constructor(props) {
|
|
|
|
|
super(props);
|
|
|
|
|
this.state = {
|
|
|
|
|
name: 'freeCodeCamp'
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
render() {
|
|
|
|
|
return (
|
|
|
|
|
<div>
|
|
|
|
|
{ /* Change code below this line */ }
|
|
|
|
|
<h1>{this.state.name}</h1>
|
|
|
|
|
{ /* Change code above this line */ }
|
|
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
```
|