3.0 KiB
3.0 KiB
id, challengeType, forumTopicId, title
id | challengeType | forumTopicId | title |
---|---|---|---|
5a24c314108439a4d4036162 | 6 | 301392 | 创建一个无状态的函数组件 |
Description
null
的 JavaScript 函数。需要注意的一点是,React 要求你的函数名以大写字母开头。下面是一个无状态功能组件的示例,该组件在 JSX 中分配一个 HTML 的 class:
// After being transpiled, the <div> will have a CSS class of 'customClass'
const DemoComponent = function() {
return (
<div className='customClass' />
);
};
因为 JSX 组件代表 HTML,所以你可以将几个组件放在一起以创建更复杂的 HTML 页面,这是 React 提供的组件架构的关键优势之一,它允许你用许多独立的组件组成 UI。这使得构建和维护复杂的用户界面变得更加容易。
Instructions
MyComponent
的函数。完成此函数,使其返回包含一些文本字符串的单个div
元素。
注意: 文本被视为是div
的子元素,因此你将不能使用自闭合标签。
Tests
tests:
- text: <code>MyComponent</code>应该返回 JSX。
testString: assert((function() { const mockedComponent = Enzyme.mount(React.createElement(MyComponent)); return mockedComponent.length === 1; })());
- text: <code>MyComponent</code>应该返回一个<code>div</code>元素。
testString: assert((function() { const mockedComponent = Enzyme.mount(React.createElement(MyComponent)); return mockedComponent.children().type() === 'div' })());
- text: <code>div</code>元素应该包含一个文本字符串。
testString: assert((function() { const mockedComponent = Enzyme.mount(React.createElement(MyComponent)); return mockedComponent.find('div').text() !== ''; })());
Challenge Seed
const MyComponent = function() {
// change code below this line
// change code above this line
}
After Test
ReactDOM.render(<MyComponent />, document.getElementById('root'))
Solution
const MyComponent = function() {
// change code below this line
return (
<div>
Demo Solution
</div>
);
// change code above this line
}