3.5 KiB
3.5 KiB
id, title, challengeType, isRequired, forumTopicId, localeTitle
| id | title | challengeType | isRequired | forumTopicId | localeTitle |
|---|---|---|---|---|---|
| 5a24c314108439a4d403617c | Use the Lifecycle Method componentWillMount | 6 | false | 301423 | Использовать компонент Lifecycle MethodWillMount |
Description
componentWillMount() componentDidMount() componentWillReceiveProps() shouldComponentUpdate() componentWillUpdate() componentDidUpdate() componentWillUnmount() Следующие несколько уроков будут посвящены некоторым основным shouldComponentUpdate() использования этих методов жизненного цикла.
Instructions
componentWillMount() вызывается перед методом render() когда компонент монтируется в DOM. Запишите что-нибудь на консоль в componentWillMount() - вы можете открыть консоль своего браузера для просмотра вывода.
Tests
tests:
- text: <code>MyComponent</code> should render a <code>div</code> element.
testString: assert((function() { const mockedComponent = Enzyme.mount(React.createElement(MyComponent)); return mockedComponent.find('div').length === 1; })());
- text: <code>console.log</code> should be called in <code>componentWillMount</code>.
testString: assert((function() { const lifecycle = React.createElement(MyComponent).type.prototype.componentWillMount.toString().replace(/ /g,''); return lifecycle.includes('console.log('); })());
Challenge Seed
class MyComponent extends React.Component {
constructor(props) {
super(props);
}
componentWillMount() {
// change code below this line
// change code above this line
}
render() {
return <div />
}
};
After Tests
ReactDOM.render(<MyComponent />, document.getElementById('root'))
Solution
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 />
}
};