3b Flashcards

1
Q

38.

A

E

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What was our third point on becoming a good React developer?

A

What changes when state changes.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What does a good React developer do?

A

Decides what changes when state changes.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Why don’t we add state to SearchBox?

const SearchBox = ({ placeholder, handleChange }) => {
   < input 
      className="search"
      type="search" 
      placeholder={placeholder} 
      onChange={handleChange} 
    / >
}

It looks like what we’re doing is adding the onChange, giving it a function and this function goes up a level and letting our App.js know that there was an event. But SearchBox, shouldn’t that have these search field state?

Why wouldn’t we put state here?

Why don’t we change the state in SearchBox and instead we trigger whenever an event happens to handleChange up in App.js?

A

because our other component CardList needs to have that information.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

How did we set up filteredMonsters?

A

By using the search field state to filter based on the user input

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Show how you can think about the way the app is set up?

A

App.js
/ \
CardList SearchBox

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

It the app was set up like this, why would it be bad was only on SearchBox?

           App.js
          /         \ CardList           SearchBox
A

There’s no way for the CardList component to find out about it.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Why couldn’t the CardList component find out about state if state was set up in SearchBox and the app was set up like this?

           App.js
          /         \ CardList           SearchBox
A

Because of one way data flow data can only flow one way. So there’s no way for the search field state to move up and down to the CardList component.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Where do we want to put state?

           App.js
          /         \ CardList           SearchBox
A

In a place where it gives us access to whatever needs it. In this case in App.js.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What is it called when you place state in a component that gives it access to whatever needs it

A

Lifting state up.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

How do we architect things in React?

A

COPY FIRST

When an event happens on a component, let’s say a user clicks or types something (in SearchBox for example), well that event triggers the React event system and says, hey something has happened, create an event handler like handle input or handle change and it’s going to go up and let the component that has that state know that hey something has happened you need to update state, and how far up that goes, let’s say we had a component underneath SearchBox, we notify the component that has the state as far up as we need to in order to pass down that information to the components that need that information.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

q

A

q

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

q

A

q

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

q

A

q

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

q

A

q

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

q

A

q

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

q

A

q

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

q

A

q

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

q

A

q

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
20
Q

q

A

q

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
21
Q

q

A

q

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
22
Q

q

A

COPY FIRST

But I want you to pay attention that you want to decide as a React developer what changes what events that the users take, how far up it goes to let the state know and you want to move the state in a high enough position so that we are still able to pass down that information to different nodes.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
23
Q

q

A

q

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
24
Q

q

A

q

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
25
Q

q

A

q

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
26
Q

q

A

q

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
27
Q

q

A

q

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
28
Q

If we had let’s say a state in number 3

3
/ \
4 5

that the only people that care about it is nodes 4 and 5, where would you put state?

A

There’s no point bringing up the state all the way up to the top node. You want to place the state at node 3 because all these components don’t really care about any of this.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
29
Q

39.

A

C

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
30
Q

What is a fundamental thing in not only JavaScript but also in React.

A

Writing our own methods on our class components.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
31
Q

What are we doing at the moment?

render() {
    const { monsters, searchFiled } = this.state; 
    return (
        < div className="app">
            < SearchBox
               placeholder="Text",
               handleChange={e => this.setState({ searchField: e.target.value })}
            / >
            < CardList monsters={filteredMonsters} />
        < / div >
    );
}
A

We pass in an anonymous function (e) into our SearchBox’s handleChange

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
32
Q

Why do we pass in an anonymous function (e) into our SearchBox’s handleChange?

render() {
    const { monsters, searchFiled } = this.state; 
    return (
        < div className="app">
            < SearchBox
               placeholder="Text",
               handleChange={e => this.setState({ searchField: e.target.value })}
            / >
            < CardList monsters={filteredMonsters} />
        < / div >
    );
}
A

In order to set our state.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
33
Q

What is this representing in this code?

render() {
    const { monsters, searchFiled } = this.state; 
    return (
        < div className="app">
            < SearchBox
               placeholder="Text",
               handleChange={e => this.setState({ searchField: e.target.value })}
            / >
            < CardList monsters={filteredMonsters} />
        < / div >
    );
}
A

Well this is a special keyword in JavaScript that references the context in which it’s being invoked.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
34
Q

What does this mean?

Well this is a special keyword in JavaScript that references the context in which it’s being invoked.

A

Well when I say this.state we know that I’m trying to reference the state on our component, our class component.

The only reason that JavaScript is able to say oh when we’re calling this.state I want the state object on our component is because the this keyword is set to the context of our class component.

Now the weird thing is that in JavaScript when we write our own class methods we’ll see that the this keyword actually gets bound differently depending on how we write the class method.

Now we’ve already seen class methods in our app component when we look at say the componentDidMount which is a lifecycle method as well as the render.

Inside of these methods they take this format where it’s the function name (e.g. render) the brackets (e.g. ()) and then the squiggly brackets (e.g. {}) but the this keyword is bound.

The reason that this is actually happening where the this context is getting bound inside of these methods is because when we call super we’re actually extending the functionality that exists on Component (class App extends Component) (which we get from React) that has these lifecycle methods and render. Because we didn’t write them, were borrowing them from Component.

When we borrow them from Component, Component actually sets the context of ‘this’ inside of them for us to our class component. If we were to write our own method for example to represent this method (e => this.setState({ searchField: e.target.value })) that we’re passing in we would have to be careful about how we write it.

Now let me show you now the reason in the first place we even want to rewrite this method (e => this.setState({ searchField: e.target.value })) in its own class method is because there’s a chance that we might want to use it more than one place and we don’t want to write code more than once. We just want to put it in one place pass it in and then change it in one place if we have to, and not have to change it in multiple places.

Now how we would do this is we would write this handleChange method here (above render()). Taking the same format that we see and it would take some event. Which is the synthetic event we’re getting back from our input as you remember (this.setState({ searchField: e.target.value })) and then we call this.setState in it.

handleChange(e) {
this.setState({ searchField: e.target.value })
}

And now we’re able to actually pass handleChange into our handleChange property on our SearchBox.

render() {
    const { monsters, searchFiled } = this.state; 
    return (
        < div className="app">
            < SearchBox
               placeholder="Text",
               handleChange={this.handleChange}
            / >
            < CardList monsters={filteredMonsters} />
        < / div >
    );
}

Now we can write it like this because we’re passing the whole callback through to it. e is being referenced in the function definition.

handleChange(e) {
this.setState({ searchField: e.target.value })
}

So we even though earlier we were explicitly writing it out like this.

handleChange={e => this.handleChange}

This is the same equivalent thing as

handleChange={this.handleChange}

because this.handleChange is referencing this full function with the e signature.

handleChange(e) {
this.setState({ searchField: e.target.value })
}

But if we were to save this we’ll actually see that our original conversation which is about ‘this’ context is undefined. If we start typing.

We’ll see that we get an error. It says that cannot read properties setState of undefined and it seems that ‘this’ is undefined.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
35
Q

When we write this.state what are we trying to do?

A

Trying to reference the state on our component, our class component.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
36
Q

Why is JavaScript able to say oh when we’re calling this.state I want the state object on our component?

A

Because the ‘this’ keyword is set to the context of our class component.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
37
Q

What is the weird thing in JavaScript when we write our own class methods?

A

We’ll see that the ‘this’ keyword actually gets bound differently depending on how we write the class method.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
38
Q

Where have wee seen class methods in our App component?

A

componentDidMount which is a lifecycle method as well as the render.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
39
Q

Inside of render and componentDidMount methods what format do they take?

A

The function name (e.g. render) the brackets (e.g. ()) and then the squiggly brackets (e.g. {}) but the this keyword is bound.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
40
Q

What is the reason where the ‘this’ context is getting bound inside of the render and componentDidMount methods?

A

Because when we call super we’re actually extending the functionality that exists on Component (class App extends Component) (which we get from React) that has these lifecycle methods and render. Because we didn’t write them, were borrowing them from Component.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
41
Q

What happens when we borrow render and componentDidMount from Component?

A

Component actually sets the context of ‘this’ inside of them for us to our class component. If we were to write our own method for example to represent this method (e => this.setState({ searchField: e.target.value })) that we’re passing in we would have to be careful about how we write it.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
42
Q

Why would we want to rewrite this method

(e => this.setState({ searchField: e.target.value }))

in its own class method?

A

Because there’s a chance that we might want to use it more than one place and we don’t want to write code more than once. We just want to put it in one place pass it in and then change it in one place if we have to, and not have to change it in multiple places.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
43
Q

How would we rewtie this method

(e => this.setState({ searchField: e.target.value }))

in its own class method so we can reuse it?

A

Write a handleChange method (above render()). Taking the same format that we see and it would take some event, which is the synthetic event we’re getting back from our input as you remember (this.setState({ searchField: e.target.value })) and then we call this.setState in it.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
44
Q

Show how would we rewtie this method

(e => this.setState({ searchField: e.target.value }))

in its own class method so we can reuse it?

A

handleChange(e) {
this.setState({ searchField: e.target.value })
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
45
Q

Pass handleChange into our handleChange property on our SearchBox.

render() {
    const { monsters, searchFiled } = this.state; 
    return (
        < div className="app">
            < SearchBox
               placeholder="Text",
               handleChange={e => this.setState({ searchField: e.target.value })}
            / >
            < CardList monsters={filteredMonsters} />
        < / div >
    );
}
A
render() {
    const { monsters, searchFiled } = this.state; 
    return (
        < div className="app">
            < SearchBox
               placeholder="Text",
               handleChange={this.handleChange}
            / >
            < CardList monsters={filteredMonsters} />
        < / div >
    );
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
46
Q

Why do we write it like this?

render() {
    const { monsters, searchFiled } = this.state; 
    return (
        < div className="app">
            < SearchBox
               placeholder="Text",
               handleChange={this.handleChange}
            / >
            < CardList monsters={filteredMonsters} />
        < / div >
    );
}
A

COPY FIRST

We’re passing the whole callback through to it. e is being referenced in the function definition.

handleChange(e) {
this.setState({ searchField: e.target.value })
}

So we even though earlier we were explicitly writing it out like this.

handleChange={e => this.handleChange}

This is the same equivalent thing as

handleChange={this.handleChange}

because this.handleChange is referencing this full function with the e signature.

handleChange(e) {
this.setState({ searchField: e.target.value })
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
47
Q

q

A

q

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
48
Q

q

A

q

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
49
Q

q

A

q

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
50
Q

q

A

q

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
51
Q

But if we were to save this code what would we see?

render() {
    const { monsters, searchFiled } = this.state; 
    return (
        < div className="app">
            < SearchBox
               placeholder="Text",
               handleChange={this.handleChange}
            / >
            < CardList monsters={filteredMonsters} />
        < / div >
    );
}
A

‘this’ context is undefined. If we start typing.

We’ll see that we get an error. It says that cannot read properties setState of undefined and it seems that ‘this’ is undefined.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
52
Q

Why when we run this code do we see an error?

handleChange(e) {
this.setState({ searchField: e.target.value })
}

render() {
    const { monsters, searchFiled } = this.state; 
    return (
        < div className="app">
            < SearchBox
               placeholder="Text",
               handleChange={this.handleChange}
            / >
            < CardList monsters={filteredMonsters} />
        < / div >
    );
}
A

Well this is actually has to do with the way the functions behave in JavaScript. when we put handleChange

handleChange(e) {
this.setState({ searchField: e.target.value })
}

on our App all it knows is that if we say this.handleChange inside the context of our app, it points to this function

handleChange(e) {
this.setState({ searchField: e.target.value })
}

but it doesn’t actually set the context of ‘this’.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
53
Q

Why isn’t the context of ‘this’ set in this code?

handleChange(e) {
this.setState({ searchField: e.target.value })
}

render() {
    const { monsters, searchFiled } = this.state; 
    return (
        < div className="app">
            < SearchBox
               placeholder="Text",
               handleChange={this.handleChange}
            / >
            < CardList monsters={filteredMonsters} />
        < / div >
    );
}
A

JavaScript, by default, doesn’t set its scope of ‘this’ on functions.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
54
Q

How do you make JavaScript set the scope of ‘this’ in this code?

handleChange(e) {
this.setState({ searchField: e.target.value })
}

render() {
    const { monsters, searchFiled } = this.state; 
    return (
        < div className="app">
            < SearchBox
               placeholder="Text",
               handleChange={this.handleChange}
            / >
            < CardList monsters={filteredMonsters} />
        < / div >
    );
}
A

You have to actually explicitly state what context you want ‘this’ to be. For us, we want ‘this’ to be our App component and the method in which we can do it is to define it in our constructor.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
55
Q

Show how you would explicitly state what context you want ‘this’ to be in this code.

handleChange(e) {
this.setState({ searchField: e.target.value })
}

render() {
    const { monsters, searchFiled } = this.state; 
    return (
        < div className="app">
            < SearchBox
               placeholder="Text",
               handleChange={this.handleChange}
            / >
            < CardList monsters={filteredMonsters} />
        < / div >
    );
}
A

constructor() {
super();

this.setState = {
    monsters: [],
    searchField: ''
}; }
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
56
Q

You have to actually explicitly state what context you want ‘this’ to be. For us, we want ‘this’ to be our App component and the method in which we can do it is to define it in our constructor. Turn this into a question

A

Because our constructor is the code that runs first, before anything gets called.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
57
Q

What do we want to do with the ‘this’ keyword in any methods we write?

A

Make sure that the context of ‘this’ is correct in all of our methods before any code gets written.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
58
Q

How could we…… finish this question

A

this.handleChange = this.handle Change.bind(this);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
59
Q

What is .bind?

A

A method on any function that returns a new function where the context of ‘this’ is set to whatever we passed to it. The context of ‘this’ that we’re setting in handleChange

handleChange(e) {
this.setState({ searchField: e.target.value })
}

is the ‘this’ keyword that is defined inside of our constructor

this.handleChange = this.handle Change.bind(this);

which knows that it’s our Component.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
60
Q

What is the problem with writing this?

this.handleChange = this.handle Change.bind(this);

A

It’s a very verbose way of writing code.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
61
Q

Why is this a verbose way of writing code?

this.handleChange = this.handle Change.bind(this);

A

Because that means that for every new class method that we write

handleChange(e) {
this.setState({ searchField: e.target.value })
}

we have to bind it.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
62
Q

What can we do instead of writing

this.handleChange = this.handle Change.bind(this);

every time we write a new class method?

A

We can actually leverage ES6 arrow functions and a unique characteristic about them that allows them to set the context of ‘this’ in whatever it was that declared it in the first place.

handleChange = (e) => {
this.setState({ searchField: e.target.value })
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
63
Q

How can you use ES6 to set the context of ‘this’ in whatever it was that declared it in the first place.

A

Arrow functions automatically allow you to set ‘this’ when

(e) => {
this.setState({ searchField: e.target.value })
}

is defined.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
64
Q

So when our Component is getting run by JavaScript for the first time and it’s like oh I got to instantiate this new App class what does it do?

A

COPY FIRST

It checks inside and it sees that there’s this handleChange method

handleChange = (e) => {
this.setState({ searchField: e.target.value })
}

that points to an arrow function.

So then it defines the arrow function based on what code we’ve given it.

And the moment it sees the ‘this’ keyword

handleChange = (e) => {
this.setState({ searchField: e.target.value })
}

it’s like oh this is an arrow function. I’m going to automatically bind ‘this’ to the place where this arrow function was defined in the first place and the context of this arrow function is our App component.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
65
Q

q

A

q

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
66
Q

q

A

q

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
67
Q

q

A

q

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
68
Q

q

A

q

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
69
Q

q

A

q

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
70
Q

q

A

q

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
71
Q

q

A

q

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
72
Q

q

A

q

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
73
Q

What’s something to note about arrow function.

A

You actually cannot call .bind on them, they automatically get what’s called lexical scoping.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
74
Q

What is lexical scoping?

A

It just means that they bind the ‘this’ context

handleChange = (e) => {
this.setState({ searchField: e.target.value })
}

to the place where they were defined in the first place.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
75
Q

Why is lexical scoping handy for us?

A

We can actually see this now that it will continue to work as it did before, even though we’re not calling the bind in the constructor anymore so this is really handy for us because it saves us that extra step.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
76
Q

How could you prove lexical scoping another way?

A

COPY FIRST

Inside of our browser the tightest/highest level context is our window object.

If I were to define a function called newFunc

const newFunc = () => console.log(this);

and what we’re logging is console.log(this).

When we invoke newFunc we’ll see that ‘this’ is that window object. When JavaScript saw us define newFunc to this function and brought it into existence within our JavaScript context so we could start using it, it had to lexically scope ‘this’

const newFunc = () => console.log(this);

to the window object because the window object was the context in which our arrow function was given its existence.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
77
Q

What is this similar to?

const newFunc = () => console.log(this);

finish question.

A

COPY FIRST

This is the same case with our handleChange method

handleChange = (e) => {
this.setState({ searchField: e.target.value })
}

When handleChange was declared, which means that when JavaScript first created our App component it also defined all of the methods (render, componentDidMoun, handleChange) on our components including handleChange, and it saw that handleChange was an arrow function and because of this, when this arrow function came into existence to JavaScript, it was going to bind any references to ‘this’ inside of it, to the context in which it was defined, which is our App component just like we saw with our myFunc in the browser, that’s what happened with our handleChange. This is great because if we want to write class methods we just write them with this arrow syntax and now the context of ‘this’ is what we expected to be without us having to write all of that extensive binding code, if we were to write it the other way so with this in mind it makes it easier for us to write our class components and their methods.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
78
Q

q

A

q

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
79
Q

q

A

q

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
80
Q

q

A

q

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
81
Q

q

A

q

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
82
Q

q

A

q

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
83
Q

q

A

q

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
84
Q

q

A

q

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
85
Q

q

A

q

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
86
Q

q

A

q

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
87
Q

q

A

q

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
88
Q

q

A

q

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
89
Q

q

A

q

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
90
Q

q

A

q

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
91
Q

q

A

q

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
92
Q

q

A

q

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
93
Q

q

A

q

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
94
Q

q

A

q

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
95
Q

40.

A

E

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
96
Q

41.

A

Q

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
97
Q

What’s a good rule of thumb regarding ‘this’?

A

Use arrow functions on any class methods you define and aren’t part of React (i.e. render(), componentDidMount()).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
98
Q

42.

A

O

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
99
Q

43.

A

O

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
100
Q

How many ways are there to connect to a Git repository?

A

Two.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
101
Q

What ways are there to connect to a Git repository?

A

Through HTTPS or SSH.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
102
Q

How can you switch between the two options HTTPS and SSH

A

By clicking the switch https/ssh button after clicking clone.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
103
Q

What is something to note about HTTPS?

A

Doesn’t require setup.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
104
Q

What is the recommended way to clone according Github official documentation?

A

Via HTTPS.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
105
Q

What is SSH?

A

A unique fingerprint you generate for your computer in your terminal, which you then let your github account know about so it knows that requests from this computer using SSH (cloning/ pushing/ pulling) are safe to do.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
106
Q

44.

A

D

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
107
Q

How would you add the title?

A

Add it as an H1 here at the top of our App.js.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
108
Q

How would we get the font?

A

COPY FIRST

Now this font we can get from Google fonts.

fonts.Google.com

and we can see all these different fonts that Google allows us to use in our project.

So I’m just gonna find the one we want called Bigelow Rules.

Hit this a little plus and Google gives us a link that we can add to our index.html inside of our head.

We just added at the bottom (of the head section) and now we have access to Bigelow Rules in our project.

If we go to App.css I’m going to add a new rule for H1 and I’m going to say font family is Bigelow Rules. I’m also going to set the font size to 72 pixel and the color.

The background we’re gonna put into our index.css and this is a file that gets generated by create-react-app.

The reason why they’ve already added this styling to the body and the code is because create-react-app wants the exact same base styling across all browsers. Different browsers might run different things like a different font or a different margin or padding so create-react-app is just trying to equalize that when the project starts. Inside of this body we’re just going to add this linear gradient to our background.

Our projects not live yet but we can actually host this really easily using github pages. create-react-app has made it really easy to use the github pages service and github lets us host static web sites for free.

The first step we’ve got to do is we have to go to github and then we have to make a new repository.

Name it something.

Then copy the key, use HTTPS if you haven’t got SSH setup.

Go to the terminal and then I’m going to do two things.

First I’m going to connect our actual project to that repository.

We’ve just made by doing

git remote add origin and then the key that was copied.

Next I’m going to actually include the gh pages package by doing

yarn add gh-pages

next I’m gonna go to our package.json file and I’m gonna do two things.

The first thing I’m going to do is I’m going to add (under “private”: true;)

“homepage”: “https://(githubUserName(without brackets)).github.io/(name of the of the project(without brackets))”,

Then add two scripts.

The first one is called ptedeploy and in this predeploy I’m just going to call build because as we remember our build script builds all of our real code into the final code that our browser needs.

“predeploy”: “yarn build”,

And then I’m gonna call deploy and this one I’m going to say gh-pages/ -d built.

“deploy”: “gh-pages/ -d built”

Now this is gonna run predeploy before it runs deploy which is really awesome because npm and yarn both know to run any script that you have pre with before you run the actual script.

So it’s gonna run predeploy before it runs deploy and then it’s going to serve up up to our github project.

Now we’re gonna go back to our terminal and then we’re gonna run our new deploy script

yarn deploy

and now we actually see it runs our predeploy script first and then we’re gonna see that it actually runs our deploy script right after.

Now this is going to build all of our project files that we need for the final project and then we’re going to add

after you run yarn deploy

git add -A

and then we’re gonna write a commit message

git commit =m “adding files for github pages”

and now we’re going to push this up

git push origin master

so once this is all pushed up to our final project repository we’re gonna go back to our github and we’re gonna refresh.

Now we should see all our files in here and if we now go to the settings and when we scroll down we want to make sure that the source is actually pointing to github pages branch.

gh-pages branch

If yours is pointing to master just make sure to set it to gh-pages branch.

And now if we go to our link we see our website is live.

It’s actually served at this URL and that’s awesome because get hub lets us serve these static websites for free very easily and very quickly and you can see how if you wanted to build a quick static web site using react it’s really easy to build it and then serve it using create-react-app and giyhub pages.

Now when we actually build our final application which is a very more dynamic e-commerce web site, we’re going to actually use databases and we’re going to leverage a lot more code and github won’t be enough for us to serve.

And I’m going to show you how we’re gonna take all of these things that we learned building this application and then expand upon them in that lesson.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
109
Q

q

A

q

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
110
Q

q

A

q

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
111
Q

q

A

q

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
112
Q

q

A

q

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
113
Q

q

A

q

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
114
Q

q

A

q

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
115
Q

q

A

q

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
116
Q

q

A

q

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
117
Q

q

A

q

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
118
Q

q

A

q

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
119
Q

q

A

q

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
120
Q

q

A

q

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
121
Q

q

A

q

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
122
Q

q

A

q

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
123
Q

q

A

q

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
124
Q

q

A

q

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
125
Q

q

A

q

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
126
Q

q

A

q

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
127
Q

q

A

q

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
128
Q

q

A

q

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
129
Q

q

A

q

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
130
Q

q

A

q

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
131
Q

q

A

q

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
132
Q

q

A

q

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
133
Q

q

A

q

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
134
Q

q

A

q

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
135
Q

q

A

q

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
136
Q

q

A

q

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
137
Q

q

A

q

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
138
Q

q

A

q

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
139
Q

q

A

q

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
140
Q

q

A

q

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
141
Q

q

A

q

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
142
Q

q

A

q

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
143
Q

q

A

q

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
144
Q

q

A

q

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
145
Q

q

A

q

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
146
Q

q

A

q

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
147
Q

q

A

q

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
148
Q

q

A

q

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
149
Q

q

A

q

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
150
Q

q

A

q

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
151
Q

q

A

q

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
152
Q

q

A

q

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
153
Q

q

A

q

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
154
Q

q

A

q

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
155
Q

q

A

q

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
156
Q

q

A

q

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
157
Q

q

A

q

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
158
Q

q

A

q

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
159
Q

q

A

q

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
160
Q

45.

A

R

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
161
Q

q

A

COPY FIRST

make a new folder called pure-react

mkdir pure-react

Inside of pure react

cd pure-react

make a source folder

mkdir src

and then inside of our source folder

cd src

make an index.html file

touch index.html

and then open it up

code .

Inside of our index.html file, generate our basic HTML.

html:5 (then press return)

Go to in the body, add a div with the I.D. of root.

< div id=”root >< div / >

In our index.js file we replace that root div with the actual contents of our application.

And then include the two packages react and react-dom.

Below the the root div write a script tag where the src goes to the react project.

< script src=”link” >< script / >

The “link” is something called unpackage which lets us very easily bring in the react library and then we’re gonna do the same thing with react-dom and now our react project has both these libraries.

So why are there two? React is kind of just the API, but then the thing that actually renders out to the DOM is what ReactDOM is. Whats there reason for both? React is a great kind of engine, but you could have many different views that run on the same engine. So ReactDOM this for the web and React Native, for example, is for native Android and iOS.

So while it use it the same React engine it uses a different package other than ReactDOM to render its Android and iOS views.

So that’s why it’s such a great package because you can even find ones like react 360 if you want to build 3D stuff, like virtual reality stuff.

How could you show that it actually removes what’s inside of this root div when we actually render in our React code.

Type React not rendered between the root div.

< div id=”root >React not rendered< div / >

Open up our index.html in the browser

In the terminal
open index.html

We see that ‘React is not rendered’ appears in the browser.

Because we haven’t actually done that call where we replace the content inside of this div with our application and we haven’t built the application yet but first if we open up our dev tools we can actually see now because we pulled in both those packages, in the console we have access to both React and ReactDOM.

type React and ReactDOM into the console

These are the objects that the two packages provide into our global JavaScript namespace, once we brought those packages in

This is pretty much the same thing that we do when we import in react and react-dom into our files. It just gives us access to these two things.

Add my own script tag and start writing some JavaScript

< script >
JavaScript goes here
< script / >

So first I’m going to write a functional component called app and i’m going to return react.createElement.

const App = () => {
    return React.createElement()
}

createElement is a function on React that builds out those elements that we saw when we wrote our return, inside of for example the CardList component.

It just we returned from this function this HTML markup. createElement is actually what it’s doing but instead of using JSX this is vanilla.

So what we’ve got to do is we write in (the argument ) that we want to make a div.

const App = () => {
    return React.createElement("div")
}

You can put any other HTML tag or whatever that you’re looking for and then the second argument is an object of any attributes that I want to add to my div.

So if I wanted to add a class or an I.D. I would put them in here.

const App = () => {
    return React.createElement("div", {class: "card-list"})
}

So I can keep it empty.

const App = () => {
    return React.createElement("div", {},)
}

And then the third and last argument is any children we want to nest inside of our div.

const App = () => {
    return React.createElement("div", {},
        React.createElement("h1", 
        {}, 
        "React is rendered")
    );
};

And I’m gonna put on an H1, no attributes, and inside of our H1 I’m going to put the text ‘React is rendered’.

Right and now we have our functional component and I’m gonna call ReactDOM.render.

const App = () => {
    return React.createElement(
        "div", 
        {},
        React.createElement("h1", {}, "React is rendered")
    );
};

ReactDOM.render(React.createElement(App), document.getELementById(“root”))

NOTE: These last two arguments are completely optional.

That’s all we were doing when we rendered out < App / > component, except with JSX but now we’re just doing it with vanilla JavaScript.

That’s all the react library does for us it just helps us create these HTML elements using functions or classes.

Now let’s say I wanted to make a const person write that renders and an array.

const Person = () => {
    return React.createElement("div", {}, [
]) }

So if we want to multiple elements/multiple children inside of this div we would just use an array and inside I would do React.createElement, H1 and empty object and I’m gonna put

“john” and then I’m gonna make another one except it’s gonna be a p tag and it’s gonna say developer.

const Person = () => {
    return React.createElement("div", {}, [
        React.createElement("h1", {}, "John"),
        React.createElement("p", {}, "developer"),
    ])
}

and now instead of just rendering in our application that each one of our actors render right, I’m also going to render in multiple Persons

const App = () => {
    return React.createElement(
        "div", 
        {},
        [
            React.createElement("h1", {}, "React is rendered"),
            React.createElement(Person),
            React.createElement(Person),
            React.createElement(Person)
        ]
    );
};

I’m going to react create element person and I’m gonna do it three times.

Now if we check our application we’ll see, we rendered our person component three times.

Now we hard coded these values, but what if we wanted to do the same thing we did with our other components right. We wanted to use props. Well because it’s just a functional component as we had before we can do exactly that can pass props into this.

And instead of “John” do something like props.name right or props.occupation and these are whatever it is that the attributes are that we want for this component to take.

const Person = (props) => {
    return React.createElement("div", {}, [
        React.createElement("h1", {}, props.name),
        React.createElement("p", {}, props.occupation),
    ])
}

So now if we wanted to actually add those then we would do our name inside of that second argument with that empty object..

const App = () => {
return React.createElement(
“div”,
{},
[
React.createElement(“h1”, {}, “React is rendered”),
React.createElement(Person, {name: “John”, occupation: “Developer”}),
React.createElement(Person, {name: “Jane”, occupation: “Developer”}),
React.createElement(Person, {name: “Mark”, occupation: “Developer”})
]
);
};

Now if we look at our application we see we can pass in our props right.

That’s all we were doing when we were writing our functional components and our class components.

It’s just that with JSX we don’t have to call React.createElement constantly.

It knows to read from that html markup that we were writing and that’s why JSX is so good when we use it with React because what we did is kind of redundant.

It’s just a quality of life improvement.

Now you might be wondering how would we do it with are class components. It’s actually the exact same.

So instead of an app I’ll just make a class app extends react component.

class App extends React.Component {

}

It’s a class that exists on react that it exposes to us and then we have access that render and in that render I’m just going to return what it is that we had before

class App extends React.Component {
render() {
return React.createElement(
“div”,
{},
[
React.createElement(“h1”, {}, “React is rendered”),
React.createElement(Person, {name: “John”, occupation: “Developer”}),
React.createElement(Person, {name: “Jane”, occupation: “Developer”}),
React.createElement(Person, {name: “Mark”, occupation: “Developer”})
]
);
};
}
}

and now instead of app being a functional component we’re still rendering our class component of app. And if we look we’ll see their application stays the same.

React does for us it helps us build out these views and that’s why React and ReactDOM are such great libraries because it just helps us simply build out our views and manage figuring out what the right thing in our views is to update.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
162
Q

q

A

q

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
163
Q

q

A

q

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
164
Q

q

A

q

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
165
Q

q

A

q

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
166
Q

q

A

q

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
167
Q

q

A

q

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
168
Q

q

A

q

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
169
Q

q

A

q

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
170
Q

q

A

q

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
171
Q

q

A

q

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
172
Q

q

A

q

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
173
Q

q

A

q

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
174
Q

q

A

q

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
175
Q

q

A

q

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
176
Q

q

A

q

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
177
Q

q

A

q

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
178
Q

q

A

q

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
179
Q

q

A

q

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
180
Q

q

A

q

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
181
Q

q

A

q

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
182
Q

q

A

q

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
183
Q

q

A

q

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
184
Q

q

A

q

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
185
Q

q

A

q

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
186
Q

q

A

q

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
187
Q

q

A

q

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
188
Q

q

A

q

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
189
Q

q

A

q

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
190
Q

q

A

q

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
191
Q

q

A

q

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
192
Q

q

A

q

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
193
Q

q

A

q

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
194
Q

q

A

q

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
195
Q

q

A

q

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
196
Q

q

A

q

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
197
Q

q

A

q

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
198
Q

q

A

q

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
199
Q

q

A

q

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
200
Q

q

A

q

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
201
Q

q

A

q

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
202
Q

q

A

q

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
203
Q

q

A

q

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
204
Q

q

A

q

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
205
Q

q

A

q

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
206
Q

q

A

q

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
207
Q

q

A

q

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
208
Q

q

A

q

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
209
Q

q

A

q

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
210
Q

q

A

q

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
211
Q

q

A

q

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
212
Q

q

A

q

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
213
Q

q

A

q

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
214
Q

q

A

q

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
215
Q

q

A

q

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
216
Q

q

A

q

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
217
Q

q

A

q

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
218
Q

q

A

q

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
219
Q

q

A

q

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
220
Q

q

A

q

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
221
Q

q

A

q

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
222
Q

q

A

q

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
223
Q

q

A

q

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
224
Q

q

A

q

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
225
Q

q

A

q

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
226
Q

q

A

q

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
227
Q

q

A

q

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
228
Q

q

A

q

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
229
Q

q

A

q

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
230
Q

q

A

q

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
231
Q

q

A

q

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
232
Q

q

A

q

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
233
Q

q

A

q

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
234
Q

q

A

q

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
235
Q

q

A

q

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
236
Q

q

A

q

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
237
Q

q

A

q

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
238
Q

q

A

q

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
239
Q

q

A

q

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
240
Q

q

A

q

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
241
Q

q

A

q

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
242
Q

q

A

q

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
243
Q

q

A

q

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
244
Q

q

A

q

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
245
Q

q

A

q

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
246
Q

q

A

q

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
247
Q

q

A

q

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
248
Q

q

A

q

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
249
Q

q

A

q

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
250
Q

46.

A

L

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
251
Q

q

A

COPY FIRST

how you can update your packages.

So because I’m in the future the version of react that I’m seeing that just got released is different

from the version that we are using throughout this course right now.

The latest version of React is 16.10.2.

In the course though the version of React 16.8.6.

It’s a very minor version change but there are a couple of new things that came out in that react version.

So the first thing to do is check what version of whatever packages that you want to upgrade are that you are currently running.

The easiest way to do so is simply using inside of your terminal

yarn list

So if you type in yarn list and then the name of the packages, here I’m checking out react react-dom and react-scripts I will see the version number

yarn list react react-dom react-script

These can be upgraded.

Now instead of directly modifying the value here there is another thing that we can do to make sure that whenever we either want to upgrade we can always use the latest version.

Now the way to do that is simply to go to our package.json inside of our project.

So inside of our package that Jason you’ll see that I have inside of these two dependencies for react and react-dom 16.8.6 and 16.8.6 and 3.0.0 for react-scripts. So if you have used create-react-app to create your application as you’ve been following on this course you may see that there’s this little carat symbol ^ in front of your version. All that means is that this is a rule that tells whatever package manager we’re using whether it be yarn or NPM when ever it sees NPM upgrade or yarn upgrade to update to the latest stable non breaking version.

So if you see this carat then you see that whenever you installed your package even if it’s 16.8.6 it will try and get the latest version.

In my case though because I did not have it whenever I run yarn install or npm install it will find just the exact version which is 16.8.6.

So I’m just going to add this carrot in to all these locations.

And what this will now do is whenever I run yarn upgrade it will try and upgrade with these new rules.

yarn upgrade

The issue that you will encounter if you change your package.json and then try to simply run

yarn upgrade

is that you will see that you have an outdated lock file, it will say please run yarn install and try again. This is perfectly fine.

All this means is that you aren’t upgrading is not going to work here because we updated the rules in our package.json when we added that carrot.

Instead we have to run

yarn install

There is a yarn.lock or a package.lock inside of your application folder.

So what is this lock file? All this lock file is it an auto generated file by either NPM or a yarn that locks the version of all the packages inside of our application within a specific range based on the rules that we set inside of package.json.

So the rules being simply what we saw in the package.json file as our version number.

If I use a carrot, it’s saying that I want it to be at least 16.8.6 or greater but not breaking. What that means is that when we had it before without this carrot the yarn lock file was generated saying that the version of React and ReactDOM and react scripts are locked to exactly that version.

Now that we’ve added this carrot it gives more flexibility which means the yarn.lock file is out of date and needs to be updated.

And this lock file only updates whenever you run yarn install.

So why do we need a lock file? The lock file is simply there so that if multiple people are working on this application they are all using versions of these dependencies that don’t conflict with each other because maybe somebody is running react 17 someone is running react 16 and there is a difference in features that might be breaking in the application. The yarn.lock file just ensures that everybody is using a consistent version of these dependencies. But with that after we ran your install we generated a new lock file and from now on you can simply run yarn upgrade whenever you want to upgrade these dependencies.

We can just double check to make sure the version of these packages that we got are updated.

So if I run

yarn list react react-dom react scripts

I’ll see that they’re now at 16.10.2 for react and react-dom as well as 3.2.0 for react-scripts.

So with this I have now updated my packages to the latest version.

Now let’s see how we do this and NPM. Here I have a separate repository that is a complete clone of our last repository.

The only difference is that in this one I’m using NPM as my package manager instead of yarn.

All I have to do in order to update these packages is run npm update.

The main difference here is that npm update will force the versions to update to their latest versions.

However it’s still good to be explicit in our rules in package.json so that we know what kind of versioning we want to do.

So first let’s just check what versions we have actually installed for react react-dom and react-scripts.

npm list

and then we type out the actual package names the same way.

npm list react react-dom react-scripts

so here with this it’s just like yarn before we see that our versions are 16.8.6 for react and react-dom and 3.0.0 for react-scripts.

So in order to update this we can go into our package.json and we can add Tilda ^ in front of react, react-dom and react-scripts just to be explicit and then we simply run npm update.

One thing I want to note is that even if you don’t add the tilde it will update the version numbers and it will overwrite your package.json file to show the latest version. So npm update and then npm install are both a little different because npm install sometimes will skip over certain versions if it doesn’t see that it’s a major update.

The safest way to make sure that you actually do update all your versions if you want to is run npm update it takes a little longer.

So here we’ll see already that it’s told us that it’s updated react on to 16.10.2 react scripts the 3.2.0 and react 16.10.2.

You’ll also see at the bottom of the terminal that it says found zero vulnerabilities.

So what are our vulnerabilities. You might see this number change sometimes whether you see it as a warning in your github or maybe when you do something with your npm it’ll show you that there’s certain vulnerabilities.

vulnerabilities are essentially these minor security concerns, sometimes they’re major, most of the time they’re minor, that has to do with either a dependency that you have installed or a dependency that your packages depend upon that you yourself don’t know that you also installed.

So for example react probably has something like 60 other packages that it depends upon. And one of those might have a security concern.

The easiest way to actually fix this inside of npm is to run

npm audit fix

and this command will go through and update the versions to a version of a package where it doesn’t have the security concern anymore.

Unfortunately with yarn, this is not so easy.

While yarn does have access to a yarn audit, all it will do is it will audit all of the packages as well as those packages dependency packages to see if there’s any vulnerabilities in any of them.

If it does it will list them to you and show you at what severity.

But for you to actually update it there’s no such easy command as npm audit fix in yarn.

You’ll have to do is manually install and upgrade all of those packages that you see with a vulnerability, if you do use yarn as your main package manager. So that’s just one thing to highlight as the main difference between npm and yarn as well as how the commands work.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
252
Q

q

A

q

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
253
Q

q

A

q

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
254
Q

q

A

q

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
255
Q

q

A

q

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
256
Q

q

A

q

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
257
Q

q

A

q

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
258
Q

q

A

q

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
259
Q

q

A

q

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
260
Q

q

A

q

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
261
Q

q

A

q

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
262
Q

q

A

q

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
263
Q

q

A

q

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
264
Q

q

A

q

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
265
Q

q

A

q

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
266
Q

q

A

q

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
267
Q

q

A

q

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
268
Q

q

A

q

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
269
Q

q

A

q

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
270
Q

q

A

q

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
271
Q

q

A

q

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
272
Q

q

A

q

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
273
Q

q

A

q

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
274
Q

q

A

q

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
275
Q

q

A

q

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
276
Q

q

A

q

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
277
Q

q

A

q

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
278
Q

q

A

q

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
279
Q

q

A

q

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
280
Q

q

A

q

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
281
Q

q

A

q

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
282
Q

q

A

q

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
283
Q

q

A

q

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
284
Q

q

A

q

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
285
Q

q

A

q

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
286
Q

q

A

q

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
287
Q

q

A

q

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
288
Q

q

A

q

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
289
Q

q

A

q

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
290
Q

q

A

q

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
291
Q

q

A

q

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
292
Q

q

A

q

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
293
Q

q

A

q

294
Q

q

A

q

295
Q

q

A

q

296
Q

q

A

q

297
Q

q

A

q

298
Q

q

A

q

299
Q

q

A

q

300
Q

q

A

q

301
Q

q

A

q

302
Q

q

A

q

303
Q

q

A

q

304
Q

q

A

q

305
Q

q

A

q

306
Q

q

A

q

307
Q

q

A

q

308
Q

q

A

q

309
Q

q

A

q

310
Q

q

A

q

311
Q

q

A

q

312
Q

q

A

q

313
Q

q

A

q

314
Q

q

A

q

315
Q

q

A

q

316
Q

q

A

q

317
Q

q

A

q

318
Q

q

A

q

319
Q

q

A

q

320
Q

q

A

q

321
Q

q

A

q

322
Q

q

A

q

323
Q

q

A

q

324
Q

q

A

q

325
Q

q

A

q

326
Q

q

A

q

327
Q

q

A

q

328
Q

q

A

q

329
Q

q

A

q

330
Q

q

A

q

331
Q

q

A

q

332
Q

q

A

q

333
Q

q

A

q

334
Q

q

A

q

335
Q

q

A

q

336
Q

q

A

q

337
Q

q

A

q

338
Q

q

A

q

339
Q

q

A

q

340
Q

47.

A

R

341
Q

q

A

COPY FIRST

Look at the developer tools in Monsters Rolodex and look at our DOM.

The DOM is our representation of our actual application.

Inside of our App component that we built, we have that H1, we have that input and we have this card-list.

These are all elements that are children of this App and inside of our card-list, is those children, the card-containers.

If we were to actually look at this from a very basic representation we’ll see that it actually looks like this tree.

                            App
                         /     |     \
                     h1       |      search-box
                          card-list
                       /       |       \
            card-component-nodes

So this tree actually represents our actual DOM.

However, in order for our actual DOM to update any of the elements, it’s actually extremely slow.

So it takes the DOM a lot of energy to update any of these elements.

Now what React does is it copies this actual DOM tree and it makes it in JavaScript just like we see when we write our JSX. Our JSX is also just JavaScript that returns what looks like these HTML elements. What react will do is it’ll completely copy this actual DOM except this it will call the virtualDOM and the virtual DOM is just a complete copy of this actual DOM but in JavaScript.

In our application whenever we update any of the data that ends up being rendered in our components, let’s say our fifth monsters email or its display name changes, well React doesn’t actually want to rerender the entire DOM it only wants to affect the thing that matters.

So what we’re going to see happen is react actually copies this virtual DOM again except this time it’s going to apply the changes to the data that got updated so the monster number five. It’s going to change the name it’s gonna change the email according to however we actually changed it and then it’s going to apply those changes to this virtual DOM and see what that element that got affected would be.

So it would end up actually being this element (changes the colour to red) so it’ll actually make the change and then what it will do is it will compare this new virtual DOM to the old DOM and then it will see that two of the values are different

So then it will know, okay, let’s actually apply the DOM changes to the element that’s different and when it does that it’s actually way more performant because it doesn’t have to mess around with the DOM unless it knows exactly what elements it needs to update. It’s also able to batch multiple DOM updates together efficiently.

And that’s why react so fast because of its ability to build out these virtual DOMS and then compare and contrast against the real DOM to see what’s changed and then make the appropriate updates to exactly the right node.

Can we actually see this in action. Well we definitely can.

What we’ve got to do first is we’ve got to go back to our monsters application and we’re going to make a couple of minor changes first.

So in order to see this in action we’re actually going to add a new field to our state, our data,

this.state = {
monsters: [],
searchFiled: ‘’,
title: ‘’
}

and we’re going to call it our title and what we’re going to do is whenever that user search happens we’re actually going to also have it so that our title updates with whatever it is that are user typed in so instead of filtering out our monsters what we’re gonna do is we’re just going to display the monsters.

onSearchChange = (event) => {
this.setState({ searchField: event.target.value, title: event.target.value });
}

    return (
        < div className="App">
            < SearchBox
               placeholder="Text",
               handleChange={this.onSearchChange}
            / >
            < CardList monsters={filteredMonsters} />
        < / div >
    );
    return (
        < div className="App">
            < SearchBox
               placeholder="Text",
               handleChange={this.onSearchChange}
            / >
            < CardList monsters={Monsters} />
        < / div >
    );

Go to 7.13

We don’t want that search feature for now. We’re also going to add in this title field that we’ve just added to our state and instead of monsters Rolodex I want to show the title.

So what happens now is that whenever we type we’ll see our title update.

Now if we enable inside of our additional rules (three dots in chrome development tools > more tools> rendering ) let’s just add actually our rendering. What we’ll see if we enable paint tools (located in Chrome development tools in Rendering Tab) is that this web site will flash green in all the areas that are getting updated whenever DOM elements are updating it will change it.

So if we refresh we’ll see the whole page flashing because it’s mounting and rendering everything for the first time.

Now let’s say we actually start typing in this the search box when we type this we’ll see that only this title is getting updated. Nothing else is getting re rendered because react knows that the only thing that needs to update is this title.

This title node is the only component in our DOM that needs to be re rendered. It doesn’t touch anything else and that’s what is so great about Reacts virtual DOM and it’s efficiency when it comes to deciding what in our DOM to re render.

Now let’s go back to our application and let’s undo this, so we get our app back to the state that it was before.

Now we know that we have our monsters array on the state of our app and we passed that after we’ve modified it, after we create a new array (in the render section), but based off of this data (in the state) array using our search field to filter out the monsters that don’t match (in the return()) and we pass that into the CardList. Then our card-list.component is the one that will then map out using the data that got passed into it, the new cards.

So if we were to look at this again except let’s just focus on our application.

                            App
                         /     |     \
                     h1       |      search-box
                          card-list
                       /       |       \
            card-component-nodes

Right that monsters array sits at our app, ot gets passed in the card-list and then it generates these components.

And these end up being all of our actual components that we view.

(This is actually all of our views(don’t know what this means)) what React does when it’s actually rendering out the application is that React is based off of this data that we saw. The data from the state of our app component (the monsters array) gets passed to this card-list component which then renders out the card-component-nodes components. If, for example, whenever we type in our search field and we update and change that filtered monsters array, that gets passed into our card list then react actually re renders this entire tree because it knows that the data at this point has changed. So if it’s passing that data down to its children the children are what is going to need to be re rendered. However, if we can imagine that we moved the data of our monsters and our filtered monsters into the actual card-list component instead.

So if we took all this and we moved it into here (card-list.component) so that the data actually sits at the card-list level, well what happens if we were to do that same thing if we were to modify that array at this level right (card-list)

                            App
                         /     |     \
                     h1       |      search-box
                          card-list
                       /       |       \
            card-component-nodes

instead of it’s sitting in the App it’s now in the card-list and then we modify that array.

Well these children (card-component-nodes) would still get re rendered. React would re render any part of the tree below this node (card-list) because the data that’s used to generate these views(card-component-nodes) actually sits here (card-list) but anything above it (the parent) our App and then the siblings (h1, search-box), the search-box and the title would not re render. React has no reason to render anything above this node (card-list) because the data only affects the children below this node. This concept is called uni directional data flow.

So in React that idea of building views from data is actually very key as to why React so predictable.

So here is actually an quick image of what that looks like.

      Views
   /|             \
  /                \|  State
342
Q

q

A

q

343
Q

q

A

q

344
Q

q

A

q

345
Q

q

A

q

346
Q

q

A

q

347
Q

q

A

q

348
Q

q

A

q

349
Q

q

A

q

350
Q

q

A

q

351
Q

q

A

q

352
Q

q

A

q

353
Q

q

A

q

354
Q

q

A

q

355
Q

q

A

q

356
Q

q

A

q

357
Q

q

A

q

358
Q

q

A

q

359
Q

q

A

q

360
Q

q

A

q

361
Q

q

A

q

362
Q

q

A

q

363
Q

q

A

q

364
Q

q

A

q

365
Q

q

A

q

366
Q

q

A

q

367
Q

q

A

q

368
Q

q

A

q

369
Q

q

A

q

370
Q

q

A

q

371
Q

q

A

q

372
Q

q

A

q

373
Q

q

A

q

374
Q

q

A

q

375
Q

q

A

q

376
Q

q

A

q

377
Q

q

A

q

378
Q

q

A

q

379
Q

q

A

q

380
Q

q

A

q

381
Q

q

A

q

382
Q

q

A

q

383
Q

q

A

q

384
Q

q

A

q

385
Q

q

A

q

386
Q

q

A

q

387
Q

q

A

q

388
Q

q

A

q

389
Q

q

A

q

390
Q

q

A

q

391
Q

q

A

q

392
Q

q

A

q

393
Q

q

A

q

394
Q

q

A

q

395
Q

q

A

q

396
Q

q

A

q

397
Q

q

A

q

398
Q

q

A

q

399
Q

q

A

q

400
Q

q

A

q

401
Q

q

A

q

402
Q

q

A

q

403
Q

q

A

q

404
Q

q

A

q

405
Q

q

A

q

406
Q

q

A

q

407
Q

q

A

q

408
Q

q

A

q

409
Q

q

A

q

410
Q

q

A

q

411
Q

q

A

q

412
Q

q

A

q

413
Q

q

A

q

414
Q

q

A

q

415
Q

q

A

q

416
Q

q

A

q

417
Q

q

A

q

418
Q

q

A

q

419
Q

q

A

q

420
Q

q

A

q

421
Q

q

A

q

422
Q

q

A

q

423
Q

q

A

q

424
Q

q

A

q

425
Q

q

A

q

426
Q

q

A

q

427
Q

q

A

q

428
Q

q

A

q

429
Q

q

A

q

430
Q

q

A

q

431
Q

48.

A

A

432
Q

q

A

COPY FIRST

I’ve just started a base create-react-app project for us and the only thing I’ve done up until now is just up there is to convert App from a function to a class component.

Now what I want to do is just simply change the two a tags to buttons, and delete what’s in the button.

class App extends React.Component {
    render() {
        return (
            < div className="App" >
                < header className="App-header" >
                    < img src={logo} className="App-logo" alt="logo" />
                < p >
                    Edit 
                < p / >
                < button 
                >
                    Update State
                < / button >
                < / header >
            < div / >
        );
    }
}

And then finally we’ll create some sort of state that will display in the < p > tags. Let’s just say that we want to create again using constructor I’m going to call super, as we always do. Then finally we’ll say this state is going to equal and let’s just say Meaning of Life is going to be 47. And display the state.

class App extends React.Component {
    constructor() {
        super();
        this.state = {
            meaningOfLife: 47
        }
    }
    render() {
        return (
            < div className="App" >
                < header className="App-header" >
                    < img src={logo} className="App-logo" alt="logo" />
                < p >
                    {this.state.meaningOfLife} 
                < p / >
                < button 
                >
                    Update State
                < / button >
                < / header >
            < div / >
        );
    }
}

But we also want to update the state via the button. So we create an onClick we’ll say it’s going to be this.handleClick and we’ll create this method (handleClick) on this component (above render) we’ll say handleClick we’ll use an arrow function so that we don’t have that issue with ‘this’ keyword and then in here we’ll simply say.setState to update our meaning of life. Well let’s just incremented by 1.

class App extends React.Component {
    constructor() {
        super();
        this.state = {
            meaningOfLife: 47
        }
    }
    handleClick = () => {
        this.setState({  meaningOfLife: this.state.meaningOfLife + 1 })
    }
    render() {
        return (
            < div className="App" >
                < header className="App-header" >
                    < img src={logo} className="App-logo" alt="logo" />
                < p >
                    {this.state.meaningOfLife} 
                < p / >
                < button 
                    onClick={this.handleClick}
                >
                    Update State
                < / button >
                < / header >
            < div / >
        );
    }
}

So if I save this and go back and I click on update state I see that my state is updated every time I click.

Now here’s the thing as I’ve mentioned before the react library helps us manipulate the DOM but to be efficient and to be fast it usually batches multiple setState calls because sometimes we can have in our app multiple setState calls or multiple locations where we change the DOM. Instead of it calling setState every single time it sometimes gets smart and says oh don’t worry if there’s a lot of setStates happening I’ll batch them together and update them all into one single update for performance. Now in our case this worked.

But React actually does not guarantee that when we call this (by clicking on the update state button) and we update state that this (the number) is going to work. Why is that.

Because this call off setState is asynchronous.

handleClick = () => {
this.setState({ meaningOfLife: this.state.meaningOfLife + 1 })
{

What does that mean.

It means that when we click on the button this doesn’t happen immediately

this.setState({ meaningOfLife: this.state.meaningOfLife

When we call this.setState we give control to the React library and we say hey you take care of this you know the best time to update the state for me, I don’t care just do it for me and I’ll keep doing something else. So unlike a synchronous call that this happens immediately

this.setState({ meaningOfLife: this.state.meaningOfLife

when we call it.

Asynchronous just happens sometime in the future. We actually saw this when it the instructor mentioned the fact that if we ever wanted to check the state, let’s say after we make this call let’s say we do a console.log

handleClick = () => {
this.setState({ meaningOfLife: this.state.meaningOfLife + 1 })
console.log(this.state.meaningOfLife)
{

If we did this we’ll notice that this state of meaning of life

handleClick = () => {
this.setState({ meaningOfLife: this.state.(here?)meaningOfLife + 1 })
console.log(this.state.meaningOfLife)
{

won’t get updated even though it’s called after the setState. If I save and go back and open up the developer tools you’ll see here that when I click the button my state is 48 but it console logged 47 and it’s always 1 behind and that is because this line runs

this.setState({ meaningOfLife: this.state.meaningOfLife + 1 })

we give control to React to update the state for us but it doesn’t do it immediately. It does it asynchronously. By the time we call this line (the console.log) the state hasn’t been updated. We saw a way to get around.

We can give a second parameter to our setState call which is our callback and the callback simply takes a function so it can just have a simple arrow function here that console.logs for us.

handleClick = () => {
this.setState({ meaningOfLife: this.state.meaningOfLife + 1 },
() => console.log(this.state.meaningOfLife))
{

I save go back and I click you see that now everything is in sync.

But here’s the thing, this is actually that practice. Why is that. Well because our app is simple right now, this is working, however, because of the way setState batches work into a single update like I mentioned, if we had multiple setState calls it doesn’t guarantee that when we increment meaningOfLife that this part (meaningOfLife) will be the latest version. Maybe another part of the app modifies meaningOfLife to be something else.

So because there is no guarantee there’s a rule and react that we have to follow and the rule is this. If in your setState call you ever use this.state directly like this.

handleClick = () => {
this.setState({ meaningOfLife: THIS.STATE.meaningOfLife + 1 },
() => console.log(this.state.meaningOfLife))
{

So instead of meaningOfLife being something with state e.g. dolphins where we don’t use the state that’s completely fine and we can just give it an object

handleClick = () => {
this.setState({ meaningOfLife: ‘dolphins’ },
() => console.log(this.state.meaningOfLife))
{

But if we ever want to use this.state or this.props in our updates to calculate something then we should do something instead of what we do here which is giving it an object.

The important part is the first parameter, this object instead of an object can also be a function and it’s a function that receives two things. One is the state or to be more descriptive the previous state and then the second parameter is the previous props. So if I just wrap this in brackets since these are the parameters

handleClick = () => {
this.setState((prevState, prevProps),
() => console.log(this.state.meaningOfLife))
{

and we call this function and in here I do the exact same thing where we return an object You could add the brackets here so that I don’t have to just type in return so that we can actually return the object here. If I do this I can say,

MeaningOfLife and say this.state.meaningOfLife + 1.

handleClick = () => {
this.setState((prevState, prevProps) => {
return {meaningOfLife: this.state.meaningOfLife + 1}
},
() => console.log(this.state.meaningOfLife))
{

MeaningOfLife just like this so this whole function

(prevState, prevProps) => {
return {meaningOfLife: this.state.meaningOfLife + 1}
},

is our first parameter and our second parameter is here

() => console.log(this.state.meaningOfLife))

So if I do this I now can use this previous state (prevState) to guarantee that when I update this (meaningOfLife) this is the latest previous state before the update. So this is just best practice that you have to remember. Anytime you want to update state and there’s props or state that you need to use within the update, then you should use this syntax of using a function instead of an object.

So in here instead of calling this.state directly I simply just say previous state.

handleClick = () => {
this.setState((prevState, prevProps) => {
return {meaningOfLife: prevState.meaningOfLife + 1}
},
() => console.log(this.state.meaningOfLife))
{

So if I save this and go back and I click update you see that everything is still working properly. Now what about this prevProps. In order for us to play with this we need to have some sort of props or properties on this App component but up until now we’ve only seen App be the parent or the biggest higher level component. It’s the top of the tree. But as I mentioned previously, state can live anywhere. We don’t need to have state in just one component. We can bring it down the tree to another node. We can mix and match.

But let me show you how we can get props to the App component.

We simply go to index.js where the App component is and lets say we’ll add a increment attribute and this increment attribute will be what we increment by.

ReactDOM.render(< App increment=1 / >, document.getElementById(‘root’))

Now if I do hear one this is going to give me an error it’s going to say well I’m trying to do JSX and I have no idea what this means because remember we need to pass it some sort of a value in JavaScript which means wrapping it in brackets so this evaluates to a JavaScript expression.

ReactDOM.render(< App increment={1} / >, document.getElementById(‘root’))

If I save this I now have these props now let’s say I wanted to use the props, so now I’m going to get the increment prop so I’ll just say this.props.increment

handleClick = () => {
this.setState((prevState, prevProps) => {
return {meaningOfLife: prevState.meaningOfLife + this.props.increment}
},
() => console.log(this.state.meaningOfLife))
{

If I save this all right. This is working nicely.

Again if I wanted to just use previous props I can just make sure that it’s the latest and nothing has changed it by using previous props.

handleClick = () => {
this.setState((prevState, prevProps) => {
return {meaningOfLife: prevState.meaningOfLife + prevProps.increment}
},
() => console.log(this.state.meaningOfLife))
{

If I save it once again everything is working. Now one last thing.

Usually it’s good practice to put in here in the constructor and super to put in the props like this.

constructor(props) {
        super(props);
        this.state = {
            meaningOfLife: 47
        }
    }

Now what does this do. This allows you to actually use the props in the constructor.

What do we mean if I do here this.props I can now use this.props inside of the constructor.

constructor(props) {
        super(props);
        this.state = {
            meaningOfLife: 47
        }
    this.props
    }

So if you ever need to use this.props inside of the constructor you need to pass this here. So for example if I just simply do this.props equals props I save it and I remove the props from here.

constructor() {
        super();
        this.state = {
            meaningOfLife: 47
        }
    this.props = props
    }

You’ll notice that I’ll get an error props is not defined because I can’t use this.props this way.

Now there are occasions when that needs to happen.

So it’s always recommended that you just add props in case you ever need to do this stop props in here.

constructor(props) {
        super(props);
        this.state = {
            meaningOfLife: 47
        }
    this.props = props
    }

So let’s say for example in our state calculation we want to say that meaning of life is going to equal 47 plus this.prop.increment so I’ll originally be 48.

constructor(props) {
        super(props);
        this.state = {
            meaningOfLife: 47 + this.props.increment
        }
    }

if we save here we have previous props here for the increment

handleClick = () => {
this.setState((prevState, prevProps) => {
return {meaningOfLife: prevState.meaningOfLife + PREVPROPS.INCREMENT}
},
() => console.log(this.state.meaningOfLife))
{

if I go back you see that it’s now 48 as the default (in the browser) so that the next update will be forty nine.

So this is why we passed props in the constructor and super. Now the final thing I want to show you is that create-react-app has something called the alternate class syntax.

Now in regular JavaScript you can’t do this and there are proposals that are going to allow future versions of JavaScript to do this but you may see them in common react code basis, so I want to show you because some people don’t like this constructor having so many lines of code.

Sometimes a state is just a simple thing.

So for example if we didn’t have any of this increment and we just had the plus one here let’s say no props, well we can just remove all of this like so and just simply say that state is equal meaningOfLife which equals forty seven and that’s it.

state = {
meaningOfLife: 47 + this.props.increment
}

handleClick = () => {
this.setState((prevState, prevProps) => {
return {meaningOfLife: prevState.meaningOfLife + 1}
},
() => console.log(this.state.meaningOfLife))
{

If I do this it leaves out the constructor and initializes the state as a class field declaration.

Again this is something because of the create-react-app and the Babel compiler that it has underneath but you are going to see this in some code bases where you just want to set state really easily but you won’t have access to the props inside of here as we did with the constructor so that if I save this this should still work and it does. Keep in mind state updates are asynchronous and we want to be careful.

The rule is this.

Do you ever want to manipulate or use the state somehow after the update. Then add in a second parameter which will be a function where you can use that updated state.

() => console.log(this.state.meaningOfLife)) {

Maybe in your update and in your state update you have to use the state as part of that calculation or the props as part of the calculation then you should pass this function form

(prevState, prevProps) => {
return {meaningOfLife: prevState.meaningOfLife + 1}
}

to update the state.

Otherwise you can just use the simple object notation that we’ve seen up until now. If you get this concept this is going to take you very far in your career.

433
Q

q

A

q

434
Q

q

A

q

435
Q

q

A

q

436
Q

q

A

q

437
Q

q

A

q

438
Q

q

A

q

439
Q

q

A

q

440
Q

q

A

q

441
Q

q

A

q

442
Q

q

A

q

443
Q

q

A

q

444
Q

q

A

q

445
Q

q

A

q

446
Q

q

A

q

447
Q

q

A

q

448
Q

q

A

q

449
Q

q

A

q

450
Q

q

A

q

451
Q

q

A

q

452
Q

q

A

q

453
Q

q

A

q

454
Q

q

A

q

455
Q

q

A

q

456
Q

q

A

q

457
Q

q

A

q

458
Q

q

A

q

459
Q

q

A

q

460
Q

q

A

q

461
Q

q

A

q

462
Q

q

A

q

463
Q

q

A

q

464
Q

q

A

q

465
Q

q

A

q

466
Q

q

A

q

467
Q

q

A

q

468
Q

q

A

q

469
Q

q

A

q

470
Q

q

A

q

471
Q

q

A

q

472
Q

q

A

q

473
Q

q

A

q

474
Q

q

A

q

475
Q

q

A

q

476
Q

q

A

q

477
Q

q

A

q

478
Q

q

A

q

479
Q

q

A

q

480
Q

q

A

q

481
Q

q

A

q

482
Q

q

A

q

483
Q

q

A

q

484
Q

q

A

q

485
Q

q

A

q

486
Q

q

A

q

487
Q

q

A

q

488
Q

q

A

q

489
Q

q

A

q

490
Q

q

A

q

491
Q

q

A

q

492
Q

q

A

q

493
Q

q

A

q

494
Q

q

A

q

495
Q

q

A

q

496
Q

q

A

q

497
Q

q

A

q

498
Q

q

A

q

499
Q

q

A

q

500
Q

q

A

q

501
Q

q

A

q

502
Q

q

A

q

503
Q

q

A

q

504
Q

q

A

q

505
Q

q

A

q

506
Q

q

A

q

507
Q

q

A

q

508
Q

q

A

q

509
Q

q

A

q

510
Q

q

A

q

511
Q

q

A

q

512
Q

q

A

q

513
Q

q

A

q

514
Q

q

A

q

515
Q

q

A

q

516
Q

q

A

q

517
Q

q

A

q

518
Q

q

A

q

519
Q

q

A

q

520
Q

q

A

q

521
Q

q

A

q

522
Q

q

A

q

523
Q

q

A

q

524
Q

q

A

q

525
Q

q

A

q

526
Q

q

A

q

527
Q

q

A

q

528
Q

q

A

q

529
Q

q

A

q

530
Q

49

A

COPY FIRST

All lifecycle methods serve different purposes and they all trigger at different times in a components lifecycle.

This project really comprises of a slightly modified version of what the base project that create-react-app gives us and in it I’ve just modified our app component to have two buttons that decide on what to show for our lifecycle component.

Now I know this lifecycle component just looks like a bit of text right now but I just put it here so that you can see whether or not it’s there or not.

And what I mean by that is whenever you click this toggle lifecycle button you’ll see that that component disappears.

I’m pulling it off the DOM based on whether or not you’ve clicked or toggled the lifecycle button.

And then the update text button whenever you click we’ll just put this _hello.

And the more you click it the more _hellos get appended to that text.

This is what the code looks like.

import Lifecycles from ‘./lifecycle.component’;

class App extends React.Component {
    constructor() {
        super();
        this.state = {
            showChild: true,
            text: ''
        };
    }
    render() {
        return (
            < div className="App" >
                < header className="App-header" >
                    < img src={logo} className="App-logo" alt="logo" / >
                    < button 
                        onClick={() =>
                            this.setState(state => ({
                                showChild: !state.showChild
                            }))
                        }
                    >
                        Update Text
                    < button / >
                    {this.state.showChild ? < Lifecycles text={this.state.text} / > : null}
                < / header >
            < / div >
        );
    }
}

The folder structure it’s pretty much just the base create-react-app project.

The only difference is that we have this lifecycle component that import into our app component and then I append it right here (under the button).

The first thing that we need to notice is that our App is a class based component that has a state where the initial values are the showChild property that is a boolean with the default value being true and then this text value which is a string with the default value being an empty string.

If you ignore the div and the header tag and the image tag because these are pretty much just presentational HTML elements.

The main thing is to look at is these two buttons and then of course this code right here below the button. In the first button we have this onClick that will fire off our function that calls setState where we set the showChild property of our state equal to the opposite value of what it was currently. We do that by using this bang operator. And what this bang operator does is it evaluates any boolean or any value to the opposite value that it is. So for example if showChild is true then I want to set the state value of showChild to the opposite value meaning that it will be bang true which is equal to false. bang just says give me the opposite boolean value of whatever that value is.

So if state.show Child is false then bang false is equal to true.

It’s a really handy way for us to toggle between a true and false state by simply calling this bang operator.

So whenever you click it it will set the showChild value to the opposite value as the next state value.

In the second button we have this onClick that fires an anonymous function that will call setState that sets the state.text to whatever it currently is except adding on this extra ‘_hello’ string.

So all we do is whenever we click it our state text just gets more and more ‘_hello’ strings appended to the end of it.

Finally we have this code here

{this.state.showChild ? < Lifecycles text={this.state.text} / > : null}

that determines whether or not to hide or show our life cycles component. So this you might see is kind of odd looking. This is actually what’s called a ternary operator and it sounds like a weird thing but it’s actually just shorthand that allows us to write easier if else statements. All it is is it takes the form of a question mark and a colon with different things between it. You can see it as three parts.

The first part is what we want to evaluate as true or false. Well if you can imagine that we had some if statement.

if () {

} else {

}

The first initial if statement right where we evaluate something about whether or not it’s true or false is pretty much what this is saying in the first part of the ternary operator. That’s why we use this boolean value that we have in our state as true or false. So I’m saying if this.state.showChild it is true then I want to either render this (the lifecycle component) or this (null). So this first thing that we see after the question mark

< Lifecycles text={this.state.text} / >

is what gets evaluated if showChild, (so whatever’s on the first part ( this.state.showChild )) is true. So if showChild is true render the Lifecycles component.

If this.state.showChild is false then render null. So you can see this last section here pretty much being what goes into the else statement. so you can see this as what goes here (finish 7.03). This as what goes here and this as what goes here so it’s pretty much just a really handy and quality of life coding a syntax that allows us to write if l statements in a much easier and readable way.

531
Q

q

A

q

532
Q

q

A

q

533
Q

q

A

q

534
Q

q

A

q

535
Q

q

A

q

536
Q

q

A

q

537
Q

q

A

q

538
Q

q

A

q

539
Q

q

A

q

540
Q

q

A

q

541
Q

q

A

q

542
Q

q

A

q

543
Q

q

A

q

544
Q

q

A

q

545
Q

q

A

q

546
Q

q

A

q

547
Q

q

A

q

548
Q

q

A

q

549
Q

q

A

q

550
Q

q

A

q

551
Q

q

A

q

552
Q

q

A

q

553
Q

q

A

q

554
Q

q

A

q

555
Q

q

A

q

556
Q

q

A

q

557
Q

q

A

q

558
Q

q

A

q

559
Q

q

A

q

560
Q

q

A

q

561
Q

q

A

q

562
Q

q

A

q

563
Q

q

A

q

564
Q

q

A

q

565
Q

q

A

q

566
Q

q

A

q

567
Q

q

A

q

568
Q

q

A

q

569
Q

q

A

q

570
Q

q

A

q

571
Q

q

A

q

572
Q

q

A

q

573
Q

q

A

q

574
Q

q

A

q

575
Q

q

A

q

576
Q

q

A

q

577
Q

q

A

q

578
Q

q

A

q

579
Q

q

A

q

580
Q

q

A

q

581
Q

q

A

q

582
Q

q

A

q

583
Q

q

A

q

584
Q

q

A

q

585
Q

q

A

q

586
Q

q

A

q

587
Q

q

A

q

588
Q

q

A

q

589
Q

q

A

q

590
Q

q

A

q

591
Q

q

A

q

592
Q

q

A

q

593
Q

q

A

q

594
Q

q

A

q

595
Q

q

A

q

596
Q

q

A

q

597
Q

q

A

q

598
Q

q

A

q

599
Q

q

A

q

600
Q

q

A

q

601
Q

q

A

q

602
Q

q

A

q

603
Q

q

A

q

604
Q

q

A

q

605
Q

q

A

q

606
Q

q

A

q

607
Q

q

A

q

608
Q

q

A

q

609
Q

q

A

q

610
Q

q

A

q

611
Q

q

A

q

612
Q

q

A

q

613
Q

q

A

q

614
Q

q

A

q

615
Q

q

A

q

616
Q

q

A

q

617
Q

q

A

q

618
Q

q

A

q

619
Q

q

A

q

620
Q

q

A

q

621
Q

q

A

q

622
Q

q

A

q

623
Q

q

A

q

624
Q

q

A

q

625
Q

q

A

q

626
Q

q

A

q

627
Q

q

A

q

628
Q

q

A

q

629
Q

q

A

q

630
Q

50.

A

COPY FIRST

class Lifecycles extends React.Component {
constructor() {
super();
console.log(‘constructor!’);
}
componentDidMount() {
console.log(‘componentDidMount!’);
}
componentDidUpdate() {
console.log(‘componentDidUpdate!’);
}
componentWillUnmount() {
console.log(‘componentWillUnmount!’);
}
shouldComponentUpdate(nextProps, nextState) {
console.log(‘shouldComponentUpdate!’, nextProps);
return true
}
render() {
console.log(‘render!’);
return (
< div className=”lifecycls” >
< h3 > LifecycleComponent < / h3 >
{this.props.text}
< / div >
);
}
}

Look at this life cycles component, called Lifecycles.

It’s just a component called Lifecycles which is a class component extending the React component. So inside of here are the possible lifecycle methods that we want to look at and inside of each one I’m just logging the name of the lifecycle that it is so that when it gets called and we look in our console we’ll see the order in which they’re getting fired.

Now before we take a look into what these life cycles are there’s actually a diagram we can look at on the React web site and on this diagram it will show us in this graph form when each method fires depending on what phase the component is in.

search ‘react lifecycle method diagram’

So the first phase we gonna look at is the mounting phase. The mounting phase is the phase when the component is being put on the DOM for the first time. So when it’s mounting when it’s in the mounting phase it starts before our component is actually on our DOM, before it’s on our page. When it starts mounting it first looks at the component, so for us our Lifecycles component and then it sees the constructor and it calls the constructor first.

So inside of our constructor we have our super, and the super is pretty much just a method on all classes inside of their constructors that say if I’m extending from some other class component I want to pull in all of their methods and all of their functionality. This might sound complicated but all you need to know is that what that does is it allows our class component to have access to these lifecycle components. Because you can see it as if these lifecycle components and when they get called in the order of all this functionality is actually originally bound to this React component, this React class component that we need to borrow from.

So we’re essentially saying extend me all of that functionality into my class component called Lifecycles, so the constructor when we call super, it’s like okay this is when I want you to give me all that functionality, so that I can bind it all to my class component. Inside of the constructor as well is when we can declare our state. So when we call this.state we do it inside of our constructor specifically for this reason because this is when our state becomes initialized on our class.

class Lifecycles extends React.Component {
    constructor() {
        super();
        console.log('constructor!');
    this.state = {

    }
}

So the constructor gets called first for this reason so that our components other lifecycle methods if they need to use the state are aware of the state because it’s initialized inside of our constructor.

After the constructor call it then calls our render method and the render method is when our component tells JavaScript, okay, this is what I want you to display in terms HTML. This is also where any prop values get evaluated in the HTML

render() {
        console.log('render!');
        return (
            < div className="lifecycls" >
                < h3 > LifecycleComponent < / h3 >
                {this.props.text}
            < / div >
        );
    }

So because we’re passing in that text string this is when JavaScript like oh I’m calling the render method on this class, I’m also going to evaluate the value of this prop.text and then turn it into something that I can display in the HTML and that’s how we’re able to see that string value that would get passed in as a prop or anything for that matter. This evaluation is happening inside of our render call.

After the render comes when React actually updates the DOM, or it makes updates to our component. The main thing here is where Reacts like okay now that I know what the component looks like because of the render and I know what state and whatever things are on the class component because the constructor I’m now going to put it on the page, I’m going to mount it.

And then once it mounts the componentDidMount lifecycle method gets fired and inside of here

componentDidMount() {
console.log(‘componentDidMount!’);
}

is where it’s like okay, the component has mounted, its in its base state, it’s properly evaluated then I’m gonna call my lifecycle method componentDidMount and if we look at our application (in the console) we’ll see that that’s the order that it happens. We get the constructor first, then the render gets called and then our componentDidMount gets called.

And usually this is where after we’ve loaded our base bare class component inside of a componentDidMount is where we start to do things like our API calls.

If you remember from our earlier instance where we made an API call to get all the users for our Monster’s Rolodex and the reason for this is because we want to load the base component first before we start fetching data and updating our component so that it looks different from the base state, because we don’t know how long that API call might take, latency might be slow your connection might be poor, but this is the order and how we should leverage this lifecycle method is that we wait for the base component to mount and then we’re like okay let’s use our componentDidMount method to update it into its next state.

And this next state this updating phase is the next phase of our component after the base mounted state is on our DOM. The next phase is the updating phase.

631
Q

q

A

q

632
Q

q

A

q

633
Q

q

A

q

634
Q

q

A

q

635
Q

q

A

q

636
Q

q

A

q

637
Q

q

A

q

638
Q

q

A

q

639
Q

q

A

q

640
Q

q

A

q

641
Q

q

A

q

642
Q

q

A

q

643
Q

q

A

q

644
Q

q

A

q

645
Q

q

A

q

646
Q

q

A

q

647
Q

q

A

q

648
Q

q

A

q

649
Q

q

A

q

650
Q

q

A

q

651
Q

q

A

q

652
Q

q

A

q

653
Q

q

A

q

654
Q

q

A

q

655
Q

q

A

q

656
Q

q

A

q

657
Q

q

A

q

658
Q

q

A

q

659
Q

q

A

q

660
Q

q

A

q

661
Q

q

A

q

662
Q

q

A

q

663
Q

q

A

q

664
Q

q

A

q

665
Q

q

A

q

666
Q

q

A

q

667
Q

q

A

q

668
Q

q

A

q

669
Q

q

A

q

670
Q

q

A

q

671
Q

q

A

q

672
Q

q

A

q

673
Q

q

A

q

674
Q

q

A

q

675
Q

q

A

q

676
Q

q

A

q

677
Q

q

A

q

678
Q

q

A

q

679
Q

q

A

q

680
Q

q

A

q

681
Q

q

A

q

682
Q

q

A

q

683
Q

q

A

q

684
Q

q

A

q

685
Q

q

A

q

686
Q

q

A

q

687
Q

q

A

q

688
Q

q

A

q

689
Q

q

A

q

690
Q

q

A

q

691
Q

q

A

q

692
Q

q

A

q

693
Q

q

A

q

694
Q

q

A

q

695
Q

q

A

q

696
Q

q

A

q

697
Q

q

A

q

698
Q

q

A

q

699
Q

q

A

q

700
Q

q

A

q

701
Q

q

A

q

702
Q

q

A

q

703
Q

q

A

q

704
Q

q

A

q

705
Q

q

A

q

706
Q

q

A

q

707
Q

q

A

q

708
Q

q

A

q

709
Q

q

A

q

710
Q

q

A

q

711
Q

q

A

q

712
Q

q

A

q

713
Q

q

A

q

714
Q

q

A

q

715
Q

q

A

q

716
Q

q

A

q

717
Q

q

A

q

718
Q

q

A

q

719
Q

q

A

q

720
Q

q

A

q

721
Q

q

A

q

722
Q

q

A

q

723
Q

q

A

q

724
Q

q

A

q

725
Q

q

A

q

726
Q

q

A

q

727
Q

q

A

q

728
Q

q

A

q

729
Q

q

A

q

730
Q

51

A

COPY FIRST

After our component has been mounted onto the DOM any future updates either to the props, the state or us manually forcing an update on our component will cause our component to go into the updating phase.

So what is the updating phase and what triggers updates.

import Lifecycles from ‘./lifecycle.component’;

class App extends React.Component {
    constructor() {
        super();
        this.state = {
            showChild: true,
            text: ''
        };
    }
    render() {
        return (
            < div className="App" >
                < header className="App-header" >
                    < img src={logo} className="App-logo" alt="logo" / >
                    < button 
                        onClick={() =>
                            this.setState(state => ({
                                showChild: !state.showChild
                            }))
                        }
                    >
                        Toggle Lifecycles
                    < / button  >
                    < button 
                        onClick={() => 
                            this.setState(state => ({
                                text: state.text + '_hello'
                            }))
                        }
                    >
                        Update Text
                    < / button >    
                    {this.state.showChild ? < Lifecycles text={this.state.text} / > : null}
                < / header >
            < / div >
        );
    }
}

Well if for example inside of our App.js we’re passing in the text value off of this.state,

text={this.state.text}

into our lifecycle method

< Lifecycles text={this.state.text} / >

which for us is this string

this.state = {
showChild: true,
text: ‘’
};

where whenever we click the button

< button 
    onClick={() => 
        this.setState(state => ({
            text: state.text + '_hello'
        }))
     }
>
    Update Text
< / button >    

we just append _hello to that string, we just make it longer and longer which we can see in the browser.

Every time we update and click the ‘Update State’ button and update our state
and this new state value

{this.state.text}

gets passed into our lifecycle component.

< Lifecycles text={this.state.text} / >

our lifecycle component goes into the updating phase and it’s like oh I got new props I should probably re render my component, not remount though. Our component is already mounted. It doesn’t need to go through mounting again.

It doesn’t need to be destroyed because React doesn’t want to create new elements for it. Doing that is actually very costly on the browser but selectively changing pieces of the HTML inside of our component is way easier because all it needs to change is this.props.text

class Lifecycles extends React.Component {
constructor() {
super();
console.log(‘constructor!’);
}
componentDidMount() {
console.log(‘componentDidMount!’);
}
componentDidUpdate() {
console.log(‘componentDidUpdate!’);
}
componentWillUnmount() {
console.log(‘componentWillUnmount!’);
}
shouldComponentUpdate(nextProps, nextState) {
console.log(‘shouldComponentUpdate!’, nextProps);
return true
}
render() {
console.log(‘render!’);
return (
< div className=”lifecycls” >
< h3 > LifecycleComponent < / h3 >
{this.props.text}
< / div >
);
}
}

it doesn’t need to change the h3 it doesn’t need to change the div so React’s like oh okay I’m just going to update

this.props.text, or I’m also going to run through those lifecycle methods that might be affected in this updating phase because the props have changed and maybe these lifecycle methods do use the props to do something. That’s all up to us and how we want to write our code.

The props have changed. I’m going to start going through my update cycle. The update cycle could also be triggered if this.setState gets called inside of our component so we don’t actually call setState inside of our Lifecycles class because we don’t have state, which is fine but if we look at our App component we do have state and we do call setsState whenever we click either of the buttons.

So whenever we click one of the buttons React re-renders this component as well

class App extends React.Component {
    constructor() {
        super();
        this.state = {
            showChild: true,
            text: ''
        };
    }
    render() {
        return (
            < div className="App" >
                < header className="App-header" >
                    < img src={logo} className="App-logo" alt="logo" / >
                    < button 
                        onClick={() =>
                            this.setState(state => ({
                                showChild: !state.showChild
                            }))
                        }
                    >
                        Toggle Lifecycles
                    < / button  >
                    < button 
                        onClick={() => 
                            this.setState(state => ({
                                text: state.text + '_hello'
                            }))
                        }
                    >
                        Update Text
                    < / button >    
                    {this.state.showChild ? < Lifecycles text={this.state.text} / > : null}
                < / header >
            < / div >
        );
    }
}

it goes through the update phase of our App component as well because there might be things inside of our component that depends on the state to determine what they need to do including our render.

If for example we were displaying this.state.text inside of our component ourselves, the component needs to decide okay I should probably re render because there’s a chance that this component is rendering something on the state. So that’s why it does it.

It’s like okay the components state has changed as well, let’s also run through our update phase for our component. Finally we can also force an update by calling this.forceUpdate inside of our component anywhere.

render() {
this.forceUpdate()
return (
< div className=”App” >
< header className=”App-header” >
< img src={logo} className=”App-logo” alt=”logo” / >
< button
onClick={() =>
this.setState(state => ({
showChild: !state.showChild
}))
}

This forces the component to update everything. This should be avoided because it’s bad practice. It just forces to go through the update phase but inside of the update phase it just goes through these following methods.

Show Slide 3:45

So just like our mount, It also goes to the render again because new props comes in or new state comes in, React has to go through its process of deciding oh what in my components changed and then it’s going to update the DOM, so it’s going to update the DOM with the changes that are required in that component.

Again we’re not re mounting the component we’re just making the change to the piece of the component where it’s affected. Then it calls componentDidUpdate similar to componentDidMount.

It’s the lifecycle method that gets called after the updates have been made to the DOM and here we can see this when we click Update text and go to the concole in the browser. We see it calls render and then it calls our componentDidUpdate.

One thing I want is to look at is actually this lifecycle method (shouldComponentUpdate).

class Lifecycles extends React.Component {
constructor() {
super();
console.log(‘constructor!’);
}
componentDidMount() {
console.log(‘componentDidMount!’);
}
componentDidUpdate() {
console.log(‘componentDidUpdate!’);
}
componentWillUnmount() {
console.log(‘componentWillUnmount!’);
}
shouldComponentUpdate(nextProps, nextState) {
console.log(‘shouldComponentUpdate!’, nextProps);
return true
}
render() {
console.log(‘render!’);
return (
< div className=”lifecycls” >
< h3 > LifecycleComponent < / h3 >
{this.props.text}
< / div >
);
}
}

shouldComponentUpdate is a lifecycle method that determines whether or not this whole chain needs to happen.

And it actually exists between these things that would trigger this updating phase that happen (new props, new state or forceUpdate) and the following lifecycle methods to get triggered. shouldComponentUpdate actually sits right here (between new props, new state or forceUpdate and render). What it does is it gets the props and then the next state. When props change they come in as a parameter or when state changes it also comes in as a parameter. What happens is that based on the new props that are coming in or the new state after we’ve called setState we can determine whether or not we want to go through these other lifecycle methods including actually re rendering our component.

Because if we don’t want to render a component we don’t have to.

And how we would do this is actually if we looked at our App.js

class App extends React.Component {
    constructor() {
        super();
        this.state = {
            showChild: true,
            text: ''
        };
    }
    render() {
        return (
            < div className="App" >
                < header className="App-header" >
                    < img src={logo} className="App-logo" alt="logo" / >
                    < button 
                        onClick={() =>
                            this.setState(state => ({
                                showChild: !state.showChild
                            }))
                        }
                    >
                        Toggle Lifecycles
                    < / button  >
                    < button 
                        onClick={() => 
                            this.setState(state => ({
                                text: state.text + '_hello'
                            }))
                        }
                    >
                        Update Text
                    < / button >    
                    {this.state.showChild ? < Lifecycles text={this.state.text} / > : null}
                < / header >
            < / div >
        );
    }
}

so for example whenever setState gets called we know that what happenes is our render call it gets called in our component.

So whenever we call setState we’re actually re rendering this whole App.js component which actually ends up re rendering the Lifecycles component

< Lifecycles text={this.state.text} / >

even if we were not passing ‘text’ in. This component,

because it’s a child of our App.js gets re rendered. I can actually show you if I get rid of this prop

< Lifecycles / >

so we’re no longer passing the prop into our Lifecycles method. Even though I click the ‘Update Text’ button in the browser you will see, in the console, that our render method and then componentDidUpdate is getting called from our Lifecycles component. This is because of how Reacts node tree works. React knows that inside of this component (< Lifecycles /> component) any of the children might need to be re rendered because it’s just going to assume that everything in the render is affected possibly by the state changing or new props coming into this component (App.js?).

So whenever a component gets updated, re rendered all the children also get re rendered and this happens whenever, again as we saw, props change or setState gets called or force update gets called inside of the component.

Now the reason this is important is because let’s say we called setState on a property that our lifecycle method doesn’t care about let’s say for example we had a another state property.

this.state = {
    showChild: true,
    text: '',
    message: ''
};

For example message. And then the message value we used somewhere else inside of the component. We don’t pass it into Lifecycles as a prop we just use it somewhere else.

Well if setState gets called the updates its message we just saw that it’s going to force our component (< Lifecycles / > component?) to update and our component if the state.text hasn’t changed

< Lifecycles text={this.state.text} / >

why do we need to re render our Lifecycles component.

That’s a waste of processing power. It’s a it’s slowing down our browser.

So what we could do here is use shouldComponentUpdate(). It allows us to selectively hijack and not go through that re render. We don’t have to re render our component. Here we can see we do that by what gets returned as a boolean value from shouldComponentUpdate.

shouldComponentUpdate(nextProps, nextState) {
console.log(‘shouldComponentUpdate!’, nextProps);
return true
}

If we return true then our component will update. It will go through the cycles and it will run through that componentDidUpdate and here we actually see us logging the next props that we’re getting.

constructor!
render!
componentDidMount!
shouldComponentUpdate!
{ text : "_hello" }
render!
componentDidMount!
shouldComponentUpdate!
{ text : "_hello_hello" }

So whenever we click ‘Update Text’ we see that the text is coming through. That’s the property, the new text property that we’re getting.

But if we return false

shouldComponentUpdate(nextProps, nextState) {
console.log(‘shouldComponentUpdate!’, nextProps);
return false
}

what happens is that it doesn’t even go through any of those lifecycle methods except for shouldComponentUpdate because when we return false we’re saying I don’t want my components to re render. And the reason we would do this is for example if we were to check the nextProps that we’re getting. If we see that the string value for our text has not changed

class Lifecycles extends React.Component {
constructor() {
super();
console.log(‘constructor!’);
}
componentDidMount() {
console.log(‘componentDidMount!’);
}
componentDidUpdate() {
console.log(‘componentDidUpdate!’);
}
componentWillUnmount() {
console.log(‘componentWillUnmount!’);
}
shouldComponentUpdate(nextProps, nextState) {
console.log(‘shouldComponentUpdate!’, nextProps);
return true
}
render() {
console.log(‘render!’);
return (
< div className=”lifecycls” >
< h3 > LifecycleComponent < / h3 >
{this.props.text}
< / div >
);
}
}

then we don’t care to update them. We don’t need to. There’s no reason for us to do that.

And here we will see that if we click Update text we’re getting this call, our shouldComponentUpdate.

constructor!
render!
componentDidMount!
shouldComponentUpdate!
{ text : "_hello" }
shouldComponentUpdate!
{ text : "_hello_hello" }

But because we’re returning false it does not go through the next cycle methods that need to happen inside of our updating phase.

And the reason we would do this as I mentioned is because maybe we don’t want to re render because our text didn’t change and we can actually force this to happen if inside of our button click instead of appending hello

 < button 
    onClick={() => 
        this.setState(state => ({
            text: state.text + '_hello'
        }))
    }
    >
    Update Text
< / button >  

we just keep the text as what it is.

< button 
    onClick={() => 
        this.setState(state => ({
            text: state.text
        }))
    }
    >
    Update Text
< / button >  

But because we’re calling setState we’re forcing the state to change and because we’re forcing the state to change we’re forcing React to re render our component.

So now inside of our lifecycle methods what we can do is here we can actually say we want to return instead of false, we want to return if next props.text does not equal this.props.text.

shouldComponentUpdate(nextProps, nextState) {
console.log(‘shouldComponentUpdate!’, nextProps);
return nextProps.text != this.props.text
}

So if the current text is the same as the next prompts text then don’t re render because there’s no point to. We don’t need to re render our component because our props have not changed.

So now we’ll see if we click up a text it doesn’t update

constructor!
render!
componentDidMount!
shouldComponentUpdate!
{ text : "" }
shouldComponentUpdate!
{ text : "" }

but if we go back and we say that, okay, now the text is changing,

< button 
    onClick={() => 
        this.setState(state => ({
            text: state.text
        }))
    }
    >
    Update Text
< / button > 
< button 
    onClick={() => 
        this.setState(state => ({
            text: state.text + '_hello'
        }))
    }
    >
    Update Text
< / button > 

well then our component will update accordingly.

constructor!
render!
componentDidMount!
shouldComponentUpdate!
{ text : "_hello" }
render!
componentDidUpdate!
shouldComponentUpdate!
{ text : "_hello_hello" }

So this is mainly just for performance. If the properties coming in are not different than the props that we already have then there’s no reason to re render. Now understanding this update phase is so fundamental to us becoming better React developers because much of how code is structured and how the architecture

for React is written, is based around this update phase. THe update phase is fundamentally what the React library does for us. This is why we write components, this is why we break our code up the way it does because this update phase is how React makes our lives easier for us whenever it updates the DOM based on new properties coming in based on us calling setState or forcing updates inside of our components.

This triggers a chain reaction of, for example, our App component updating also its children, not only rendering itself but re rendering any child components it has.

And then those child components also update. But that’s the thing, if we trigger big updates like this there may not be a need to even update these components if as we saw the props do not actually change. If the props don’t change nothing about this component (shouldComponentUpdate?)needs to update. So leveraging the lifecycle methods will allow us to possibly improve the performance of our application but also give us a better idea of when we should do what with our components.