2018-10-10 18:03:03 -04:00
|
|
|
|
---
|
|
|
|
|
id: 5a24c314108439a4d4036168
|
2021-03-05 08:43:24 -07:00
|
|
|
|
title: 从零开始写一个 React 组件
|
2018-10-10 18:03:03 -04:00
|
|
|
|
challengeType: 6
|
2020-09-18 00:13:42 +08:00
|
|
|
|
forumTopicId: 301424
|
2021-01-13 03:31:00 +01:00
|
|
|
|
dashedName: write-a-react-component-from-scratch
|
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
|
|
|
|
|
2021-03-05 08:43:24 -07:00
|
|
|
|
你已经了解了 JSX 和 React 组件的基础知识,是时候自己编写一个组件了。 React 组件是 React 应用程序的核心组成部分,因此熟练编写它们是非常重要的。 记住,典型的 React 组件是 ES6 `class`,它扩展了 `React.Component`。 它有一个返回 HTML(从 JSX 返回)或 `null` 的渲染方法, 这是 React 组件的基本形式。 理解了这一点之后,就可以开始构建更复杂的 React 项目了。
|
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-03-05 08:43:24 -07:00
|
|
|
|
定义一个 `MyComponent` 类,它是 `React.Component` 的扩展。 它的渲染方法应该返回一个 `div`,其中包含一个 `h1` 标签,标签文本为:`My First React Component!`。 准确使用此文本,大小写和标点符号也要考虑。 确保也调用组件的构造器。
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
2021-03-05 08:43:24 -07:00
|
|
|
|
使用 `ReactDOM.render()` 把该组件渲染到 DOM 中。 有一个 `id='challenge-node'` 的 `div` 可供渲染。
|
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-03-05 08:43:24 -07:00
|
|
|
|
应该有一个名为 `MyComponent` 的 React 组件。
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
|
```js
|
|
|
|
|
(getUserInput) =>
|
|
|
|
|
assert(
|
2021-02-06 04:42:36 +00:00
|
|
|
|
__helpers
|
|
|
|
|
.removeWhiteSpace(getUserInput('index'))
|
2020-12-16 00:37:30 -07:00
|
|
|
|
.includes('classMyComponentextendsReact.Component{')
|
|
|
|
|
);
|
2018-10-10 18:03:03 -04:00
|
|
|
|
```
|
|
|
|
|
|
2021-03-05 08:43:24 -07:00
|
|
|
|
`MyComponent` 应该包含一个 `h1` 标签,标签的文本为 `My First React Component!`,区分大小写并有标点符号。
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
|
```js
|
|
|
|
|
assert(
|
|
|
|
|
(function () {
|
|
|
|
|
const mockedComponent = Enzyme.mount(React.createElement(MyComponent));
|
|
|
|
|
return mockedComponent.find('h1').text() === 'My First React Component!';
|
|
|
|
|
})()
|
|
|
|
|
);
|
|
|
|
|
```
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
2021-03-05 08:43:24 -07:00
|
|
|
|
`MyComponent` 应该渲染到 DOM。
|
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(document.getElementById('challenge-node').childNodes.length === 1);
|
2018-10-10 18:03:03 -04:00
|
|
|
|
```
|
2020-08-13 17:24:35 +02:00
|
|
|
|
|
2021-03-05 08:43:24 -07:00
|
|
|
|
`MyComponent` 应该有一个构造器,里面调用了传参 `props` 的 `super` 函数。
|
2021-01-13 00:22:19 +08:00
|
|
|
|
|
|
|
|
|
```js
|
|
|
|
|
assert(
|
|
|
|
|
MyComponent.toString().includes('MyComponent(props)') &&
|
|
|
|
|
MyComponent.toString().includes('_super.call(this, props)')
|
|
|
|
|
);
|
|
|
|
|
```
|
2021-01-13 03:31:00 +01:00
|
|
|
|
|
|
|
|
|
# --seed--
|
|
|
|
|
|
|
|
|
|
## --seed-contents--
|
|
|
|
|
|
|
|
|
|
```jsx
|
|
|
|
|
// Change code below this line
|
|
|
|
|
```
|
|
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
|
# --solutions--
|
|
|
|
|
|
2021-01-13 03:31:00 +01:00
|
|
|
|
```jsx
|
|
|
|
|
// Change code below this line
|
|
|
|
|
class MyComponent extends React.Component {
|
|
|
|
|
constructor(props) {
|
|
|
|
|
super(props);
|
|
|
|
|
}
|
|
|
|
|
render() {
|
|
|
|
|
return (
|
|
|
|
|
<div>
|
|
|
|
|
<h1>My First React Component!</h1>
|
|
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
ReactDOM.render(<MyComponent />, document.getElementById('challenge-node'));
|
|
|
|
|
```
|