(License: CC BY-SA 4.0)
Prev - React Workshops, Next - React Authentication with Firebase Workshop
A JavaScript library for building user interfaces for web as well as mobile applications developed by a team at Facebook in 2011.
Component = A piece of the UI
Independent, isolated, and reuable components, which become the building blocks to a more complex user interface.
In react, webpages are just the combination of many children components.
Every React application has at least one component known as the Root Component (Most of the time named the App component).
This component represents the entire application and is where all the child components are displayed.
Here we see several components
|
Take a look at all the components (which are all the rectangles with red borders).
The App (or root component) Component is the page itself that is displaying all the components.
It will install the tools npm
, to install 3rd party plugins, and npx
, for running tools.
npm
is the node package manager, andnpx
is the package runner that allows us to run programs without installing.Open your command prompt/terminal on Windows/Mac:
$ node -v
This is to see our version of Node.js on our device, if there’s an error, it didnt succesfully download.
Create your app:
$ npx create-react-app first-react-app
(first-react-app
is the name of our project)
If it works, congrats you’ve created your very first react app.
Start VS Code to see its contents:
$ cd first-react-app
$ code src/App.js
Run it with:
$ npm start
Modify the text in VScode to see changes!
JSX is an extension of Javascript that allows HTML to appear inside the code.
A React component is basically a JS function that returns HTML code:
import logo from './logo.svg';
import './App.css';
function App() {
return (
<div className="App">
...
</div>
);
}
export default App;
which is forbidden in regular Javascript.
insert before App()
:
function MyComponent() {
return (
<p>
Say my name!
</p>
)
}
(you can replace MyComponent
with any word you like)
Then, insert before the closing </header>
<MyComponent />
(make sure it’s the same function name as above.)
Now you can save and observe the output.
Props (short for properties): is an object of arbitrary inputs a React function accepts as the first argument.
State: is the data that changes over the lifetime of a specific instance in a react component.
props
as arguments to the component function.2 + 3
), you can place that code into a function and any
dynamic values that code used before can be accepted as arguments
(such as add(2, 3)
).props
allow passing arguments in HTML.When we define (insert before App()):
function Add(props) {
return (
<div>
{props.n1} + {props.n2} = {props.n1 + props.n2}
</div>
)
}
({}
bring back Javascript into the HTML)
Then, insert before </header>
<Add n1={2} n2={3} />
Becomes:
2+3=5
Start by adding on the top:
import { useState } from 'react';
We will add a button to flip a coin. Let’s create state for the coin:
const [coin, setCoin] = useState(0);
Here, coin
becomes the variable holding the value and setCoin
is
the function we will use to update the state. useState(0)
sets the
initial value.
To flip the coin, we need to call setCoin()
:
setCoin((coin + 1) % 2)
Put it in the onClick
action of an HTML button as a callback:
<button onClick={ () => setCoin((coin + 1) % 2) } > Flip </button>
function Flip() {
const [coin, setCoin] = useState(0);
return (
<div>
{coin}
<button onClick={ () => setCoin((coin + 1) % 2) }
> Flip </button>
</div>
)
}
and add a line to the App()
function as well, below <Add>
:
<Flip />
You will get:
0
Clicking the button will flip the coin value between 0 and 1.
First install the React Router in the console/terminal:
$ npm install react-router-dom
Then, follow tutorial. Some examples:
<Switch>
<Route exact path="/">
<Home />
</Route>
<Route path="/about">
<About />
</Route>
<Route path="/dashboard">
<Dashboard />
</Route>
</Switch>
<Router>
<div>
<ul>
<li>
<Link to="/">Home</Link>
</li>
<li>
<Link to="/about">About</Link>
</li>
<li>
<Link to="/dashboard">Dashboard</Link>
</li>
</ul>
</div>
</Router>