Enter Your Email Address to Watch This Lesson

Your link to unlock this lesson will be sent to this email address.

Unlock this lesson and all 833 of the free egghead.io lessons, plus get React content delivered directly to your inbox!

Existing egghead members will not see this. Sign in.

Just one more step!

Check your inbox for an email from us and click link to unlock your lesson.

Manage React Component State with Lifecycle Methods

1:58 React lesson by

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.

Get the Code Now
click to level up

egghead.io comment guidelines


The previous lesson introduced the React component lifecycle mounting and unmounting. In this lesson you will learn some simple uses for these hooks.


I thought props were suppose to be readonly.

Why are you using setProps here instead of setState?

In reply to egghead.io

While using setProps may be considered an anti-pattern when allowing children components to update their own props in this case we have a single component for our entire app where using setProps or setState are both perfectly acceptable methods for triggering a render. The use of setProps here was actually added in the previous video merely to illustrate it's existence and usage.

In reply to Christopher

Why do you store btnStyle directly on the object, instead of on the state?


are these code examples supposed to be working with the latest version of react? none of the code samples are working for me.


It's working for me, using react 0.13.2. Here is a working example of the above code:

In reply to Fareez

getting an error saying 'component's children should no be mutated'. in button...


I really like the lessons, everything is becoming clear now.

In componentWillMount, we have access to our state and our props but we do not have access to the DOM representation of our component yet because it has not been placed into the DOM. To illustrate that, we're going to go ahead and update our state.

I'm going to add a key of m for multiplier. I'm going to set that to 2. Then in our <button> here, which is incrementing our state of val, I'm going to multiply that by this.state.m. We'll save that.


import React from 'react';

class App extends React.Component {
  constructor() {
    this.state = {val: 0}
    this.update = this.update.bind(this)
    this.setState({val: this.state.val + 1})
    this.setState({m: 2})
    return <button onClick={this.update}>
        {this.state.val * this.state.m}
export default App

We can see when we mount, our componentWillMount, render, and didMount have fired. When I click on this, you can see it's multiplying our val by two. We were able to set the state or intercept the state before it actually rendered.

In componentDidMount, we have access to our component in the DOM. We could log out, ReactDOM.findDOMNode, and we could just pass at this.

componentDidMount() {

When we mount it, we can see we've got our actual HTML element right here in the console. In componentWillUnmount, we have the opportunity to clean up any running processes that we might need to. Here in componentDidMount, I'm going to set a variable called this.inc to equal an interval and I'm going to set that for every half-second and it's just going to call this.update.

  this.inc = setInterval(this.update,500)

Before I clean that up I'm going to go ahead and load it up in the browser, and when I mount it we're going to see that the render method is being called every time we run this.update. When I unmount it, because I didn't clean it up we're getting an error here that says, We're setting state on something that isn't actually available any longer.

To utilize componentWillUnmount, I'm just going to say clearInternval(this.inc) for increment and now, we mount. We can see our value is being incremented, that interval is being fired. When we unmount, we've cleaned that up, we get our componentWillUnmount, and we're done.


Joel's Head
Why are we asking?