Files
freeCodeCamp/curriculum/challenges/russian/03-front-end-libraries/react/write-a-react-component-from-scratch.russian.md

3.2 KiB
Raw Blame History

id, title, challengeType, isRequired, forumTopicId, localeTitle
id title challengeType isRequired forumTopicId localeTitle
5a24c314108439a4d4036168 Write a React Component from Scratch 6 false 301424 Напишите компонент реакции с нуля

Description

Теперь, когда вы изучили основы компонентов JSX и React, пришло время написать компонент самостоятельно. Реагирующие компоненты являются основными строительными блоками приложений React, поэтому важно хорошо ознакомиться с их написанием. Помните, что типичным компонентом React является class ES6, который расширяет React.Component . Он имеет метод рендеринга, который возвращает HTML (из JSX) или null . Это основная форма компонента React. Как только вы это хорошо поймете, вы будете готовы начать строительство более сложных проектов React.

Instructions

Определите класс MyComponent который расширяет React.Component . Его метод render должен возвращать div , содержащий тег h1 с текстом: My First React Component! в этом. Используйте этот текст точно, дело и значение пунктуации. Не забудьте также вызвать конструктор для вашего компонента. Передайте этот компонент в DOM с помощью ReactDOM.render() . Для вас доступен div с id='challenge-node' .

Tests

tests:
  - text: There should be a React component called <code>MyComponent</code>.
    testString: getUserInput => assert(getUserInput('index').replace(/\s/g, '').includes('classMyComponentextendsReact.Component{'));
  - text: <code>MyComponent</code> should contain an <code>h1</code> tag with text <code>My First React Component!</code> Case and punctuation matter.
    testString: assert((function() { const mockedComponent = Enzyme.mount(React.createElement(MyComponent)); return mockedComponent.find('h1').text() === 'My First React Component!'; })());
  - text: <code>MyComponent</code> should render to the DOM.
    testString: assert(document.getElementById('challenge-node').childNodes.length === 1);

Challenge Seed

// change code below this line

Solution

// 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'));