In this lesson, we're going to talk about the updating lifecycle methods that are available to us in our components.
To get us started, I'm going to return a button. It's going to have an onclick handler of this.update.bind. to this. It's inner html is going to be this.props.val. We're going to get that val from default props. I'm going to say app.default props equals, we'll set our val to zero.
We're going to go ahead and create this update method. We're going to get a little tricky with this one. We're actually going to rerender our entire component.
I'm going to say ReactDOM.render app. I'm going to target the same div that we're targeting index js. Here, we're going to say val equals, and we're going to say this props.val plus one. Every time we render it, we're going to increment by one that value of val. If we save that and try it out, each time we click on this, it get incremented by one because of its new props.
I'm going to add to this some state. I'm going to set up our constructor. We'll call super to get our context. I'm going to set the state to equal. We're going to have a value here for increasing. I'm going to set that to false. What we want to do is determine if the new prop that's coming in of val is increasing from the previous prop or not.
The first updating lifecycle method we can look at is component will receive props. This means that new properties are coming in. We get access props right here, with this variable, next prop. What we're going to do is we're going to set our state of increasing to next props.val is greater than this props.val.
Then here, in our render method, we'll simply cancel log this state.increasing. The first time through, that should not be true. Each subsequent rerender it should be true.
We'll save that and open up our dev tools. Here's the first firing. We'll click on this guy, and from here on out, it's going to be true, because we're constantly increasing that value.
The next updating phase we can look at is should component update? This is going to take in our next props, as well as our next state. What I'm going to do here is say if the next props not val is a multiple of five, then, we'll go ahead and update it.
Here, it's simply returning a true or false. It's important to note that this doesn't prevent our state, and of course, not our props, from being updated. It simply prevents a rerender. If I save that, two, three, four five, we get our update, one, two, three, four, five, we get our update. Again, the state has actually been changed.
The last one we can take a look at is component did update, which takes in our previous props, as well as our previous state. Here, we'll just logout prev props, and the actual previous props. Here in the browser, we'll click one, two, three, four, five. Whoops.
Let's go ahead and get the val off of that guy. Prev props val. One, two, three, four, five. We can see our previous prop was four, that indicates that our state is actually being updated while our props are actually being updated. We can see from our state we've got true, and our prop is now four. If we go one, two, three, four, five, we get nine.