Files
freeCodeCamp/curriculum/challenges/russian/03-front-end-libraries/react/use-react-to-render-nested-components.russian.md

4.8 KiB
Raw Blame History

id, title, challengeType, isRequired, forumTopicId, localeTitle
id title challengeType isRequired forumTopicId localeTitle
5a24c314108439a4d4036165 Use React to Render Nested Components 6 false 301420 Использовать React для вставки вложенных компонентов

Description

Последняя задача показала простой способ составления двух компонентов, но есть много разных способов компоновки компонентов с помощью React. Компонентный состав является одной из мощных возможностей React. Когда вы работаете с React, важно начать думать о своем пользовательском интерфейсе с точки зрения таких компонентов, как пример приложения в последнем вызове. Вы разбиваете свой пользовательский интерфейс на свои основные строительные блоки, и эти части становятся компонентами. Это помогает отделить код, отвечающий за пользовательский интерфейс, от кода, ответственного за обработку вашей логики приложения. Это может значительно упростить разработку и сопровождение сложных проектов.

Instructions

В редакторе кода есть два функциональных компонента, называемых TypesOfFruit и Fruits . Возьмите TypesOfFruit компонент и компоновать его, или гнездо его, в пределах Fruits компонента. Затем возьмите компонент Fruits и TypesOfFood его в компонент TypesOfFood . Результатом должен быть дочерний компонент, вложенный в родительский компонент, который вложен в собственный родительский компонент!

Tests

tests:
  - text: The <code>TypesOfFood</code> component should return a single <code>div</code> element.
    testString: assert(Enzyme.shallow(React.createElement(TypesOfFood)).type() === 'div');
  - text: The <code>TypesOfFood</code> component should return the <code>Fruits</code> component.
    testString: assert(Enzyme.shallow(React.createElement(TypesOfFood)).props().children[1].type.name === 'Fruits');
  - text: The <code>Fruits</code> component should return the <code>TypesOfFruit</code> component.
    testString: assert(Enzyme.mount(React.createElement(TypesOfFood)).find('h2').html() === '<h2>Fruits:</h2>');
  - text: The <code>TypesOfFruit</code> component should return the <code>h2</code> and <code>ul</code> elements.
    testString: assert(Enzyme.mount(React.createElement(TypesOfFood)).find('ul').text() === 'ApplesBlueberriesStrawberriesBananas');

Challenge Seed

const TypesOfFruit = () => {
  return (
    <div>
      <h2>Fruits:</h2>
      <ul>
        <li>Apples</li>
        <li>Blueberries</li>
        <li>Strawberries</li>
        <li>Bananas</li>
      </ul>
    </div>
  );
};

const Fruits = () => {
  return (
    <div>
      { /* change code below this line */ }

      { /* change code above this line */ }
    </div>
  );
};

class TypesOfFood extends React.Component {
  constructor(props) {
    super(props);
  }

  render() {
    return (
      <div>
        <h1>Types of Food:</h1>
        { /* change code below this line */ }

        { /* change code above this line */ }
      </div>
    );
  }
};

After Tests

ReactDOM.render(<TypesOfFood />, document.getElementById('root'))

Solution

const TypesOfFruit = () => {
  return (
    <div>
      <h2>Fruits:</h2>
      <ul>
        <li>Apples</li>
        <li>Blueberries</li>
        <li>Strawberries</li>
        <li>Bananas</li>
      </ul>
    </div>
  );
};

const Fruits = () => {
  return (
    <div>
      { /* change code below this line */ }
        <TypesOfFruit />
      { /* change code above this line */ }
    </div>
  );
};

class TypesOfFood extends React.Component {
  constructor(props) {
    super(props);
  }

  render() {
    return (
      <div>
        <h1>Types of Food:</h1>
        { /* change code below this line */ }
        <Fruits />
        { /* change code above this line */ }
      </div>
    );
  }
};