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.