All right. 1
So here's the starting file for the React components practice. And the aim of the game is to separate 2
out all of this code into a separate component. By the end 3
you should have a single component which is called App inside your index 4
.js. And then you should have two separate JSX files, a App.jsx and a heading.js 5
x. And you should be able to use what you learnt from the previous lessons to separate out a lot of this 6
logic into different components. 7
Pause the video, fork the sandbox and see how you get on. 8
All right. 9
So here I've got the original code and we're rendering a single h1 element which is the heading which 10
is showing up right here. And what we want to do is we want to clear out all of the code from our index. 11
js so that it's only rendering a App component. 12
So let's go ahead and cut this h1 element and move it over to our heading.jsx. And also 13
the code that's required to render the heading, 14
so all of this logic we're going to cut it out of our index. 15
jsx and move it over here as well. 16
Now remember we need our React module in order to render JSX. So let's go ahead and import React 17
from "react". 18
And now we're going to create a function that is going to be called heading. 19
And this function is going to enclose all of this code. 20
So I'm going to cut this part out and then move it to the bottom here. 21
The final thing we need to do is we need to give our function an output, so tell it what it needs to 22
return 23
and it's of course going to be this h1 with class name of heading with style that's dynamically 24
determined by a code above and the greeting is also determined by the time of day using our Javascript 25
code inside this adding component. The final thing we need to do with our heading component is to export 26
this heading function. So let's go ahead and export default heading and now we can save it and use it 27
inside our App.js. So our App. 28
js is going to be the starting point of our component tree. 29
So it's going to import from React and then it's simply going to have a function called App and we're 30
just going to return one component in this case. 31
So normally you would probably have a div with multiple components. 32
In our case we could have the div or we could simply leave it out because all we want to return is just 33
that custom heading component which of course comes from the heading.jsx file which we'll need 34
to import if we actually want to use it. 35
Finally we're going to export our app as the default export from this file and then we're going to go 36
back over into our index.js and we're going to try and use that app element inside the ReactDOM render 37
method. 38
So let's go ahead to 39
import App from ./App. 40
And remember what I said in the last lesson that normally you would have a directory called components 41
to store all of these JSX components 42
and in that case then instead of ./App, it would be ./components/App and we get 43
back exactly the same website that we had in the beginning, but now we've got a starting point for our 44
app which is the App.jsx component and then inside that App.jsx 45
we've got a child component which is the heading that is worked out from the custom code right here. 46
So did you manage to get it right? 47
If not be sure to have a review of the last lesson. 48
But in the next lesson, we're going to do a deep dive on this import and export keyword that we've been 49
seeing a lot in the last few lessons. 50
And this is something that comes from ES6. 51
If you're interested in learning more about how imports and exports and how modules work in ES6, then 52
head over to the next lesson. 53
But if you're already really familiar with this and you've already used it and you're comfortable with 54
it, then feel free to skip the next lesson and continue in the curriculum. 55
So whichever way you choose, 56
I'll see you on the next lesson.