React
React
---------------------------------
Setting up Development Environment
reload VS Code
Other Extension
Prettier
developed by Esben Peterson
Video 3
Now lets use create react app package to create a
new react app
In a terminal type
create-react-app and give the application a name
the name of a app
create-react-app react-app
install react and all the third parties libraries we
need
its going to install light weight development server
Web pack for bundling our files
Babel for compiling our Javascript code as well as
other useful tools
So when you create an application you create react
app you have to do any configuration zero-config
setup all the configuration is done for you however
If you want to coustomize the configuration for
production environment you can use
npm run eject command
so after completing of this command
if you see Happy Hacking you are set to go
Basics of React
What Component is what State is what Prop
React is an efficient, declarative, and flexible open-source
single JavaScript library for building simple, fast and
scalable front-ends application and building user
interfaces (UIs) created and maintained by facebook
Code:
Create a Class component called App
class App extends React.Component { // this syntax is to
define or create a React component as a plain JavaScript
class
React "components" are using either Javascript functions
or classes here we use a React Class
To define a React component class,
you have to make a class and you need to extends
React.Component
the component's name must start with an upper case
letter and extend React.Component statement,
here we name our component App so
this statement creates an inheritance to
React.Component, and gives your component access to
React.Component's functions followed by sets of curly
braces
In Short: the class itself was creating something called a
React component .
A component is one of the basics pieces of react and in
this project we build tremendous amount of Component
so class component is a JavaScript class that extends
React.Component which has a render method to the
class
----------------------
and then inside this class body I am going to say
state = { //Javascript object and this object is going to have
one properties inside of it name
btn_color which as a value of 'button_red'
remember ending with semi colon at the end of the line
btn_color: 'button_red'
};
//Event Handler
onClick = (event) => { // OnClick method assign this method to
and outputting it arrow function this arrow function called
within a single argument that we refer to as event and inside
the body of this function we
// Changing state
this.setState({
btn_color: 'button-grey'
})
}
<button
onClick={this.onClick} // Same capital letter * Set
click handler */
className={this.state.btn_color} // refrencing
button red inside of the button its read click me
>
Click Me!
</button>
</div>
.button-red {
background-color:red;
color:white; #text will be white
}
.button-grey {
background-color:grey;
color:white;
}
when we click it will turn grey
that's a react app code
to change the state of react component on click
code:
class App extends React.Component {
state = {
btn_color: 'button-red'
};
btn_color: 'button-grey'
})
render(){
return(
<div>
<button
onClick={this.onClick}
className={this.state.btn_color}
>
Click Me!
</button>
</div>
HTML
<div id ='root' />
CSS
.button-red {
background-color:red;
color:white;
.button-grey {
background-color:grey;
color:white;
function OakAcademy() {
const pi = 2;
if(true) {
let pi = 3.14;
console.log (pi)
}
pi=1;
console.log (pi)
}
OakAcademy()
----------------------------------------
function OakAcademy() {
let pi = 2;
if(true) {
let pi = 3.14;
console.log (pi)
}
pi=1;
console.log (pi)
}
OakAcademy()
nodejs.org/en/download
so you can see after installing their are so many npm packages
as we learning javascript
we have different version of javasaacript
you see some error messages when you run this command
or when you try to visit your application on the browser
we now have a react application been run iside of a terminal as
long as its running in the terminal we can visit localhost 3000
and thats the address of our app
stop the react app using command cntrl c
stop the other server or running the server on the other port
number
instead of localhost run your server on local network IP
Functional Components
we are going to make a couple of changes to our project
inside of my cod editor OPEN UP get into the src directory
Remember that our goal is to understand how jsx works
we delete all the codes inside of this files and we are going to
create a new files which we are going to experiment with and
that way we start from scratch to get a better understanding
how jsx works
so after highlighting every files inside the src directory and
delete all of it
now the scr directory is empty i am going to
create a new file inside the src called index.js
the name of the file is indeed very special make sure you name
the file index.js
so when we create a new file and flip get to the browser
our react application is running in local host with port 3000
if we refresh the page so now we have a completely blank page
so now we have to figure it out how to use React?
to get to appear some content on the screen using react
and we do that by playing jsx with certain rules
You need to know the rules to breakup
or you need to break the rules to learn
It's a three steps process basically and we are going to follow
one two three to show up content to show up on the screen
this is what you have to do
we begin right now with Step Number 1 import
we have to import react and react dom library
and in order to do so i am going to write a code
import React from 'react';
imort ReactDOM from 'react-dom'
make sure you have a capital DOM in your import statement
just we have it here
so now we going to talk about little
what import statement s are and why we have to use it here
in codepen online tool
when we create a project we didn't have to write an import
statement
we just added React and ReactDOM to our project
and now we are using it with create react app and we are using
a bundler system called web pack
we will talk about what webpack is and why we are using it
later
but right now all we need to understand is that every file we
are going to create inside our project is essentially all tiny little
universe
any code we write inside this files is not going to be
automatically shared or made available to other files or sent
vice versa so
in another words we want to gain access any libraries or any
code inside that we write our project we have to write import
system like the one we use in index.js file
lets look diagram
the Next step is from which is keyword to pull a code from a file
or a dependencies and then finally we will list either it the
dependencies we want to import from or a path to the file
which we want to import
in case we are importing a file that we created ourselves
)
so here we got function based component together and sholw
it on the screen in our browser
here we take pour app component we just created and put into
JSX tags essentially look like HTML tags and comma and second
argument is a reference to the DOM element that already exist
inside our HTML file
------------------------------------------------------------------------------------
Learn more about JSX How JSX Works? and How we interact
with it?
So in the last section, we finally got some content to appear on
the screen of our browser.Ah, but that's just not good enough,
is it?Oh, no way, so.
Stay tuned, because now that we've got a working application
put together, we're going to start to
focus on the JSX that we had written into our app component in
the index as file.
First, I just want to explain what exactly JSX is.
You've had a little bit of an introduction, but I want you to know
that when you look at this stuff,
as I've said many times, it looks like HTML, but in reality it's not.
So I want to give you a quick reminder about something that
we spoke about just a little bit ago, so
remember that I told you that when we write ES2015 code,
which you and I like to do as developers, we do not send it
directly into a user's browser because their browser might not
have E 2015 or 2016 or 2017 fully supported.
So in order to get around that, we made use of the dependency
called Bable.
Now we made use of Bable to take that ES 2015, 2016 or
whatever code and convert it down to normal ES fiveJavaScript.
All right.
We got that out the way now before JSX
actually gets sent down to a user's browser.
It gets converted into normal looking JavaScript code.
And you might be immediately curious as to how do we take
something that looks like this right here
is at https://babeljs.io/
Oh, right on here.
And we're going to find the right out button right here at the
very top.
So now on this interface, we can write a certain amount of code
here on the left hand side.
Now, this can be E as 2015 2016 code that contains JSX as well.
And I'm going to paste it back over here on the left hand side.
All right, so as you can see, that div right there and that kind of
jazz looking thing actually
gets turned into JavaScript function call.
Actually, it's a call to react, not create element.
So the first argument is a string that indicates that type of
element that we want to create.
And then the third element.
Is the text that we want to show inside of that div.
So every time that you see JSX the actual writing gets turned
into this big function called right here.
So I'm going to put a couple of Eli and maybe those I have texts
of their own like Hi.
Now, when we take a look at this thing and it's JSM form, it's
pretty easy to tell what's going on
if we're familiar with how HTML works.
Now, you can take a glance at this over here and understand
that this app component is going to create
a device that has a ul and three li
Now, also, you can very quickly see what text those allies are
going to contain.
However, if you look at the converted results over here on the
right hand side, it's a lot harder
to understand the exact structure and content of what this
component is.
So the entire purpose of JSX is to just be able to allow us to
write out these different function calls
}
}
Routing