2018-10-10 18:03:03 -04:00
|
|
|
|
---
|
|
|
|
|
id: 5a24c314108439a4d403616e
|
2021-03-05 08:43:24 -07:00
|
|
|
|
title: 使用 this.props 访问 Props
|
2018-10-10 18:03:03 -04:00
|
|
|
|
challengeType: 6
|
2020-09-18 00:13:42 +08:00
|
|
|
|
forumTopicId: 301375
|
2021-01-13 03:31:00 +01:00
|
|
|
|
dashedName: access-props-using-this-props
|
2018-10-10 18:03:03 -04:00
|
|
|
|
---
|
|
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
|
# --description--
|
|
|
|
|
|
2021-03-05 08:43:24 -07:00
|
|
|
|
前几项挑战涵盖了将 props 传递给子组件的基本方法。 但是,倘若接收 prop 的子组件不是无状态函数组件,而是一个 ES6 类组件又当如何呢? ES6 类组件访问 props 的方法略有不同。
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
2021-03-05 08:43:24 -07:00
|
|
|
|
任何时候,如果要引用类组件本身,可以使用 `this` 关键字。 要访问类组件中的 props,需要在在访问它的代码前面添加 `this`。 例如,如果 ES6 类组件有一个名为 `data` 的 prop,可以在 JSX 中这样写:`{this.props.data}`。
|
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
|
|
|
|
|
2021-11-17 06:20:53 -08:00
|
|
|
|
在父组件 `App` 中渲染 `Welcome` 组件的一个实例。 在这里,给 `Welcome` 一个 `name` 的 prop,并给它赋值一个字符串。 在 `Welcome` 的子节点里,访问 `strong` 标签内的 `name` prop。
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
|
# --hints--
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
2021-11-17 06:20:53 -08:00
|
|
|
|
`App` 组件应返回单个 `div` 元素。
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
|
```js
|
|
|
|
|
assert(
|
|
|
|
|
(function () {
|
2021-11-17 06:20:53 -08:00
|
|
|
|
const mockedComponent = Enzyme.mount(React.createElement(App));
|
2020-12-16 00:37:30 -07:00
|
|
|
|
return mockedComponent.children().type() === 'div';
|
|
|
|
|
})()
|
|
|
|
|
);
|
|
|
|
|
```
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
2021-11-17 06:20:53 -08:00
|
|
|
|
`App` 的子组件应该是 `Welcome` 组件。
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
|
```js
|
|
|
|
|
assert(
|
|
|
|
|
(function () {
|
2021-11-17 06:20:53 -08:00
|
|
|
|
const mockedComponent = Enzyme.mount(React.createElement(App));
|
2018-10-10 18:03:03 -04:00
|
|
|
|
return (
|
2021-11-17 06:20:53 -08:00
|
|
|
|
mockedComponent.children().childAt(0).name() === 'Welcome'
|
2018-10-10 18:03:03 -04:00
|
|
|
|
);
|
2020-12-16 00:37:30 -07:00
|
|
|
|
})()
|
|
|
|
|
);
|
2018-10-10 18:03:03 -04:00
|
|
|
|
```
|
|
|
|
|
|
2021-11-17 06:20:53 -08:00
|
|
|
|
`Welcome` 组件应该有一个名为 `name` 的 prop。
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
|
|
|
|
```js
|
2020-12-16 00:37:30 -07:00
|
|
|
|
assert(
|
|
|
|
|
(function () {
|
2021-11-17 06:20:53 -08:00
|
|
|
|
const mockedComponent = Enzyme.mount(React.createElement(App));
|
|
|
|
|
return mockedComponent.find('Welcome').props().name;
|
2020-12-16 00:37:30 -07:00
|
|
|
|
})()
|
|
|
|
|
);
|
2018-10-10 18:03:03 -04:00
|
|
|
|
```
|
|
|
|
|
|
2021-11-17 06:20:53 -08:00
|
|
|
|
`Welcome` 组件应显示你在 `strong` 标签中作为 `name` 属性传递的字符串。
|
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
|
|
|
|
assert(
|
|
|
|
|
(function () {
|
2021-11-17 06:20:53 -08:00
|
|
|
|
const mockedComponent = Enzyme.mount(React.createElement(App));
|
2020-09-18 00:13:42 +08:00
|
|
|
|
return (
|
2020-12-16 00:37:30 -07:00
|
|
|
|
mockedComponent.find('strong').text() ===
|
2021-11-17 06:20:53 -08:00
|
|
|
|
mockedComponent.find('Welcome').props().name
|
2020-09-18 00:13:42 +08:00
|
|
|
|
);
|
2020-12-16 00:37:30 -07: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
|
2021-11-17 06:20:53 -08:00
|
|
|
|
ReactDOM.render(<App />, document.getElementById('root'))
|
2021-01-13 03:31:00 +01:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
## --seed-contents--
|
|
|
|
|
|
|
|
|
|
```jsx
|
2021-11-17 06:20:53 -08:00
|
|
|
|
class App extends React.Component {
|
2021-01-13 03:31:00 +01:00
|
|
|
|
constructor(props) {
|
|
|
|
|
super(props);
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
render() {
|
|
|
|
|
return (
|
|
|
|
|
<div>
|
|
|
|
|
{ /* Change code below this line */ }
|
2021-11-17 06:20:53 -08:00
|
|
|
|
<Welcome />
|
2021-01-13 03:31:00 +01:00
|
|
|
|
{ /* Change code above this line */ }
|
|
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2021-11-17 06:20:53 -08:00
|
|
|
|
class Welcome extends React.Component {
|
2021-01-13 03:31:00 +01:00
|
|
|
|
constructor(props) {
|
|
|
|
|
super(props);
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
render() {
|
|
|
|
|
return (
|
|
|
|
|
<div>
|
|
|
|
|
{ /* Change code below this line */ }
|
2021-11-17 06:20:53 -08:00
|
|
|
|
<p>Hello, <strong></strong>!</p>
|
2021-01-13 03:31:00 +01:00
|
|
|
|
{ /* Change code above this line */ }
|
|
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
```
|
|
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
|
# --solutions--
|
|
|
|
|
|
2021-01-13 03:31:00 +01:00
|
|
|
|
```jsx
|
2021-11-17 06:20:53 -08:00
|
|
|
|
class Welcome extends React.Component {
|
2021-01-13 03:31:00 +01:00
|
|
|
|
constructor(props) {
|
|
|
|
|
super(props);
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
render() {
|
|
|
|
|
return (
|
|
|
|
|
<div>
|
2021-11-17 06:20:53 -08:00
|
|
|
|
{ /* Change code below this line */ }
|
|
|
|
|
<p>Hello, <strong>{this.props.name}</strong>!</p>
|
|
|
|
|
{ /* Change code above this line */ }
|
2021-01-13 03:31:00 +01:00
|
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2021-11-17 06:20:53 -08:00
|
|
|
|
class App extends React.Component {
|
2021-01-13 03:31:00 +01:00
|
|
|
|
constructor(props) {
|
|
|
|
|
super(props);
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
render() {
|
|
|
|
|
return (
|
|
|
|
|
<div>
|
2021-11-17 06:20:53 -08:00
|
|
|
|
{ /* Change code below this line */ }
|
|
|
|
|
<Welcome name="Quincy"/>
|
|
|
|
|
{ /* Change code above this line */ }
|
2021-01-13 03:31:00 +01:00
|
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
```
|