Files
freeCodeCamp/curriculum/challenges/ukrainian/03-front-end-development-libraries/react/use-the-lifecycle-method-componentwillmount.md
2023-08-16 08:18:51 -07:00

3.2 KiB
Raw Blame History

id, title, challengeType, forumTopicId, dashedName
id title challengeType forumTopicId dashedName
5a24c314108439a4d403617c Використайте метод життєвого циклу componentWillMount 6 301423 use-the-lifecycle-method-componentwillmount

--description--

Компоненти React мають декілька спеціальних методів, які надають можливість виконувати дії у визначений час їхнього життєвого циклу. Вони називаються методами життєвого циклу, або перехоплювачами життєвого циклу, і дозволяють виловити компонент у визначений час. Це може бути перед відтворенням, оновленням, отриманням пропсу, від’єднанням тощо. Ось список декількох головних методів життєвого циклу: componentWillMount() componentDidMount() shouldComponentUpdate() componentDidUpdate() componentWillUnmount(). В наступнних уроках ми розглянемо деякі основні випадки їх використання.

Примітка: метод життєвого циклу componentWillMount буде нерекомендований у майбутній версії 16.X і видалений з версії 17. Дізнайтесь більше у цій публікації

--instructions--

Метод componentWillMount() викликається перед методом render(), коли компонент підключений до DOM. Напишіть щось на консолі в межах componentWillMount() (можливо, вам знадобиться відкрити консоль браузера, щоб побачити вивід).

--hints--

MyComponent має відтворити елемент div.

assert(
  (function () {
    const mockedComponent = Enzyme.mount(React.createElement(MyComponent));
    return mockedComponent.find('div').length === 1;
  })()
);

console.log потрібно викликати в componentWillMount.

assert(
  (function () {
    const lifecycle = React.createElement(MyComponent)
      .type.prototype.componentWillMount.toString()
      .replace(/ /g, '');
    return lifecycle.includes('console.log(');
  })()
);

--seed--

--after-user-code--

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

--seed-contents--

class MyComponent extends React.Component {
  constructor(props) {
    super(props);
  }
  componentWillMount() {
    // Change code below this line

    // Change code above this line
  }
  render() {
    return <div />
  }
};

--solutions--

class MyComponent extends React.Component {
  constructor(props) {
    super(props);
  }
  componentWillMount() {
    // Change code below this line
    console.log('Component is mounting...');
    // Change code above this line
  }
  render() {
    return <div />
  }
};