--- id: 5a24c314108439a4d403617c title: Use the Lifecycle Method componentWillMount challengeType: 6 isRequired: false --- ## Description
React components have several special methods that provide opportunities to perform actions at specific points in the lifecycle of a component. These are called lifecycle methods, or lifecycle hooks, and allow you to catch components at certain points in time. This can be before they are rendered, before they update, before they receive props, before they unmount, and so on. Here is a list of some of the main lifecycle methods: componentWillMount() componentDidMount() componentWillReceiveProps() shouldComponentUpdate() componentWillUpdate() componentDidUpdate() componentWillUnmount() The next several lessons will cover some of the basic use cases for these lifecycle methods.
## Instructions
The componentWillMount() method is called before the render() method when a component is being mounted to the DOM. Log something to the console within componentWillMount() - you may want to have your browser console open to see the output.
## Tests
```yml tests: - text: MyComponent should render a div element. testString: 'assert((function() { const mockedComponent = Enzyme.mount(React.createElement(MyComponent)); return mockedComponent.find("div").length === 1; })(), "MyComponent should render a div element.");' - text: console.log should be called in componentWillMount. testString: 'assert((function() { const lifecycle = React.createElement(MyComponent).type.prototype.componentWillMount.toString().replace(/ /g,""); return lifecycle.includes("console.log("); })(), "console.log should be called in componentWillMount.");' ```
## Challenge Seed
```jsx class MyComponent extends React.Component { constructor(props) { super(props); } componentWillMount() { // change code below this line // change code above this line } render() { return
} }; ```
### After Test
```js console.info('after the test'); ```
## Solution
```js 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
} }; ```