freeCodeCamp/curriculum/challenges/chinese/03-front-end-libraries/react/write-a-react-component-fro...

2.5 KiB
Raw Blame History

id title challengeType isRequired videoUrl localeTitle
5a24c314108439a4d4036168 Write a React Component from Scratch 6 false 从Scratch写一个React组件

Description

现在您已经学习了JSX和React组件的基础知识现在是时候自己编写一个组件了。 React组件是React应用程序的核心构建块因此非常熟悉编写它们非常重要。请记住典型的React组件是扩展React.Component的ES6 class 。它有一个返回HTML来自JSXnull的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