2.5 KiB
2.5 KiB
id | title | challengeType | isRequired | videoUrl | localeTitle |
---|---|---|---|---|---|
5a24c314108439a4d4036168 | Write a React Component from Scratch | 6 | false | 从Scratch写一个React组件 |
Description
React.Component
的ES6 class
。它有一个返回HTML(来自JSX)或null
的render方法。这是React组件的基本形式。一旦你理解了这一点,你就会准备开始构建更复杂的React项目。 Instructions
React.Component
的类MyComponent
。它的render方法应该返回一个div
,其中包含一个带有文本的h1
标签: My First React Component!
在里面。准确使用此文本,案例和标点符号很重要。确保也调用组件的构造函数。使用ReactDOM.render()
将此组件呈现给DOM。有一个div
, id='challenge-node'
可供您使用。 Tests
tests:
- text: 应该有一个名为<code>MyComponent</code>的React组件。
testString: 'getUserInput => assert(getUserInput("index").replace(/\s/g, "").includes("classMyComponentextendsReact.Component{"), "There should be a React component called <code>MyComponent</code>.");'
- text: <code>MyComponent</code>应该包含带有文本<code>My First React Component!</code>的<code>h1</code>标签<code>My First React Component!</code>案例和标点符号问题。
testString: 'assert((function() { const mockedComponent = Enzyme.mount(React.createElement(MyComponent)); return mockedComponent.find("h1").text() === "My First React Component!"; })(), "<code>MyComponent</code> should contain an <code>h1</code> tag with text <code>My First React Component!</code> Case and punctuation matter.");'
- text: <code>MyComponent</code>应该呈现给DOM。
testString: 'assert(document.getElementById("challenge-node").childNodes.length === 1, "<code>MyComponent</code> should render to the DOM.");'
Challenge Seed
// change code below this line
Solution
// solution required