# Lifecycle mounting - React

**URL:** <https://forum.freecodecamp.org/t/lifecycle-mounting-react/73467>\
**Category:** General\
**Created:** [January 6, 2017, 8:41pm UTC](https://forum.freecodecamp.org/t/lifecycle-mounting-react/73467 "2017-01-06T20:41:42Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![xavierartot](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/xavierartot/32/160720_2.png) [@xavierartot](https://forum.freecodecamp.org/u/xavierartot)\
**Post date:** [January 6, 2017, 8:41pm UTC](https://forum.freecodecamp.org/t/lifecycle-mounting-react/73467/1 "2017-01-06T20:41:43Z")

</div>

Hi,  
I’m trying to use componentDidMount to see a dom element?

```
    console.log( ReactDOM.findDOMNode(this) );

```

I got an error in the console:

```
socket.js:110 Uncaught TypeError: Converting circular structure to JSON(…)

import React from 'react'
import ReactDOM from 'react-dom'

class LifeCycleMountingUsage extends React.Component {
  constructor(){
    super()
    this.state = {
      count: 0
    }
    this.update = this.update.bind(this)//attach bind(this) to the method update()
  }
  update = () => {
    this.setState({count: this.state.count+1});
  }
  componentWillMount () {//before the component is mounted in DOM
    console.log('componentWillMount')
    this.setState({ m : 2});
  }
  render() {
    console.log('render');
    return (
      <div>
        <button onClick={this.update} className='button'>
          { this.state.count * this.state.m }
        </button>
      </div>
    );
  }
  componentDidMount () {//the component been mounted to the DOM
    console.log('componentDidMount')
    console.log( ReactDOM.findDOMNode(this) );
  }
  componentWillUnmount () {//unmount the component
    console.log('componentWillUnmount')
  }
}

class Wrapper extends React.Component {
  mount = () => {
    ReactDOM.render(<LifeCycleMountingUsage />, document.querySelector('#a') )
  }
  unmount = () => {
    ReactDOM.unmountComponentAtNode(document.querySelector('#a') )
  }
  render() {
    return (
      <div>
        <button onClick={()=> this.mount()} className='button'>Mount</button>
        <button onClick={this.unmount.bind(this)} className='button'>Unmount</button>
        <div id='a'></div>
      </div>
    );
  }
}
export default Wrapper
```

---

<div class="post-metadata">

**Author:** ![jenovs](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/jenovs/32/60963_2.png) [@jenovs](https://forum.freecodecamp.org/u/jenovs)\
**Post date:** [January 6, 2017, 8:57pm UTC](https://forum.freecodecamp.org/t/lifecycle-mounting-react/73467/2 "2017-01-06T20:57:24Z")

</div>

> [@xavierartot](#):
>
> mount = () =\> {  
> ReactDOM.render(\<LifeCycleMountingUsage /\>, document.querySelector(‘#a’) )  
> }  
> unmount = () =\> {  
> ReactDOM.unmountComponentAtNode(document.querySelector(‘#a’) )  
> }

This part is confusing. ReactDOM.render inside a component 🤔

---

<div class="post-metadata">

**Author:** ![xavierartot](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/xavierartot/32/160720_2.png) [@xavierartot](https://forum.freecodecamp.org/u/xavierartot)\
**Post date:** [January 6, 2017, 9:00pm UTC](https://forum.freecodecamp.org/t/lifecycle-mounting-react/73467/3 "2017-01-06T21:00:09Z")

</div>

well it works?  
I was starting React this week, I mount the component LifeCycleMountingUsage in the “a element”, because I’m in an other component.

Edit:  
I did this tutorial:

> **[Manage React Component State with Lifecycle Methods](https://egghead.io/lessons/react-manage-react-component-state-with-lifecycle-methods)**
>
> The previous lesson introduced the React component lifecycle mounting and unmounting. In this lesson you will learn some simple uses for these hooks and how we can interact with state.

---

<div class="post-metadata">

**Author:** ![jenovs](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/jenovs/32/60963_2.png) [@jenovs](https://forum.freecodecamp.org/u/jenovs)\
**Post date:** [January 6, 2017, 9:20pm UTC](https://forum.freecodecamp.org/t/lifecycle-mounting-react/73467/4 "2017-01-06T21:20:37Z")

</div>

Hmm, interesting.

I noticed that in tutorial they didn’t use arrow functions, but you did.

---

<div class="post-metadata">

**Author:** ![ILM](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/ilm/32/363239_2.png) [@ILM](https://forum.freecodecamp.org/u/ILM)\
**Post date:** [January 16, 2021, 5:08pm UTC](https://forum.freecodecamp.org/t/lifecycle-mounting-react/73467/5 "2021-01-16T17:08:28Z")

</div>

Closed on January 16, 2021, 5:08pm UTC


