React, sometimes referred to as a frontend JavaScript framework, is a JavaScript library created by Facebook.
React creates a VIRTUAL DOM in memory .React creates a VIRTUAL DOM in memory.
<!DOCTYPE html>
<html>
<head>
<script src="https://unpkg.com/react@18/umd/react.development.js" crossorigin></script>
<script src="https://unpkg.com/react-dom@18/umd/react-dom.development.js" crossorigin></script>
<script src="src="https://unpkg.com/@babel/standalone/babel.min.js" crossorigin></script>
<head>
const container = document.getElementById('root');
const root = ReactDOM.createRoot(container);
root.render(Hello
);
<body>
<div id="root"></div>
</body>
import React from 'react';
import ReactDOM from 'react-dom/client';
const container = document.getElementById('root');
const root = ReactDOM.createRoot(container);
root.render(<p>Hello</p>);
const myelement = (
<table>
<tr>
<th>Name</th>
</tr>
<tr>
<th>Ayman</th>
</tr>
<tr>
<th>Ahmed</th>
</tr>
</table>
);
const container = document.getElementById('root');
const root = ReactDOM.createRoot(container);
root.render(myelement);
Upgrading an existing React application to version 18 only requires two steps.
npm i react@latest react-dom@latest
// Before
import ReactDOM from 'react-dom';
ReactDOM.render(<App />, document.getElementById('root'));
// After
import ReactDOM from 'react-dom/client';
const root = ReactDOM.createRoot(document.getElementById('root'));
root.render(<App />);
class Car {
constructor(name) {
this.brand = name;
}
}
<!DOCTYPE html>
<html>
<body>
<script>
class Car {
constructor(name)
this.brand = name;
}
}
const mycar = new Car("Ford");
document.write(mycar.brand);
</script>
</body>
</html>
<html>
<body>
class Car {
constructor(name) {
this.brand = name;
}
}
present() {
return 'I have a ' + this.brand;
}
}
const mycar = new Car("Ford");
mycar.present();
</script>
</body>
</html>
<!DOCTYPE html>
<html>
<body>
<p id="demo"></p>
<script><br>
hello = () => {
return "Hello world!"
}
document.getElementById("demo").innerHTML = hello();
</script>
</body>
</html>
import React from 'react';
import ReactDOM from 'react-dom/client';
const myArray = ['apple', 'banana', 'orange'];
const myList = myArray.map((item) => <p>{item}</p>)
const container = document.getElementById('root');
const root = ReactDOM.createRoot(container);
root.render(myList);
const vehicles = ['mustang', 'f-150', 'expedition'];
const [car, truck, suv] = vehicles;
const vehicles = ['mustang', 'f-150', 'expedition'];
const [car,, suv] = vehicles;
<script>
function calculate(a, b) {
const add = a + b;
const subtract = a - b;
const multiply = a * b;
const divide = a / b;
return [add, subtract, multiply,divide];
}
const [add, subtract, multiply , divide]= calculate(4,7);
document.write("<p>Sum: " + add + "</p>");
document.write("<p>Difference " + subtract + "</p>");
document.write("<p>Product: " + multiply + "</p>");
document.write("<p>Quotient " + divide + "</p>");
</script>
</body>
</html>
const vehicleOne = {
brand: 'Ford',
model: 'Mustang',
type: 'car',
year :2024,
color: 'red'
}
myVehicle(vehicleOne);
//old way
function myVehicle(vehicleOne) {
const message = 'My' + vehicle.type + 'is a ' + vehicle.color + ' '+ vehicle.color + ' ' + vehicle.brand + ' '+ vehicle.model + ' ' ;
}
<!DOCTYPE html>
<html>
<body>
<script>
<p id="demo"></p>
const vehicleOne = {
brand: 'Ford',
model: 'Mustang',
type: 'car',
year: 2021,
color: 'red'
}
myVehicle(vehicleone);
function myVehicle ({type, color , brand , model}) {
const message = 'My ' + type + ' is a ' + color + ' ' + brand + ' ' + model + '.';
}
document.getElementById("demo").innerHTML = message;
}
</script>
</body>
</html>
You can create named exports two ways:
export const name = "Jesse"
export const age = 40
const name = "Jesse"
const age = 40
export { name, age }
const message = () => {
const name = "Jesse"
const age = 40;
return name + ' is ' + age + 'years old';
};
export default message;
<!DOCTYPE html>
<html>
<body>
<p id="demo"></p>
<script type="module">
import {name, age } from "./person.js;"
document.getElementById("demo").innerHTML = "My name is " + name;
document.getElementById("demo").innerHTML += ", I am " + age + ".";
</script>
</body>
</html>
<!DOCTYPE html>
<html>
<body>
<p id="demo"></p>
<script type="module">
import message from "./message.js";
document.getElementById("demo").innerHTML = message();
</script>
</body>
</html>
class Car extends React.component {
render() {
return Hello, this is my Car
;
}
}
function Car(){
return Hi, I am a React
}
import React from 'react';
import ReactDOM from 'react-dom/client';
function Car() {
return <h2> hi , i am a React Developer!</h2>;
}
const root = ReactDOM.createRoot(document.getElementById('root'));
root.render(<car/>);
import React form 'react';
import ReactDOM from 'react-dom/client';
function Car (props) {
return <h2<I am a {props.color} Car!</h2<;
}
const root = ReactDOM.creatRoot(document.getElementById('root'));
root.render(<car color="red"/>);
const myElement = ;
import React from 'react';
import ReactDOM from 'react-dom/client';
function Car(props) {
return <h2>I am a { props.brand }!</h2>;
}
const myElement = ;
const root = ReactDOM.createRoot(document.getElementById('root'));
root.render(myElement);
import React from 'react';
import ReactDOM from 'react-dom/client';
function Car(props) {
return <h2>I am a { props.brand }!</h2>;
}
function Garage() {
return (
<>
<h1>Who lives in my garage?</h1>
<Car brand="Ford" />
</>
);
}
const root = ReactDOM.createRoot(document.getElementById('root'));
root.render(<Garage />);
<button onClick={shoot}>Take the Shot!</button>
<button onClick={shoot}>Take the Shot!</button>
import React from 'react';
import ReactDOM from 'react-dom/client';
function Football() {
const shoot = () => {
alert("Great Shot!");
}
return (
<button onClick={shoot}>Take the shot!</button>
);
}
const root = ReactDOM.createRoot(document.getElementById('root'));
root.render(<Football />);
function MissedGoal() {
return <h1>MISSED!</h1>;
}
function MadeGoal() {
return <h1>Goal!</h1>;
}
import React from 'react';
import ReactDOM from 'react-dom/client';
function MissedGoal() {
return <h1>MISSED!</h1>;
}
function MadeGoal() {
return <h1>GOAL!</h1>;
}
function Goal(props) {
const isGoal = props.isGoal;
if (isGoal) {
return <MadeGoal/>;
}
return <MissedGoal/>;
}
const root = ReactDOM.createRoot(document.getElementById('root'));
root.render(<Goal isGoal={false} />);
import React from 'react';
import ReactDOM from 'react-dom/client';
function Car(props) {
return <li>I am a { props.brand }</li>;
}
function Garage() {
const cars = ['Ford', 'BMW', 'Audi'];
return (
<>v
<h1>Who lives in my garage?</h>
<ul>
{cars.map((car) => <Car brand={car} />)}
</ul>
</>
);
}
const root = ReactDOM.createRoot(document.getElementById('root'));
root.render(<Garage />);
/*
If you run this example in your create-react-app,
you will receive a warning that there is no "key" provided for the list items.
*/
import React from 'react';
import ReactDOM from 'react-dom/client';
function Car(props) {
return - I am a { props.brand }
;
}
function Garage() {
const cars = [
{id: 1, brand: 'Ford'},
{id: 2, brand: 'BMW'},
{id: 3, brand: 'Audi'}
];
return (
<>
Who lives in my garage?
{cars.map((car) => )}
>
);
}
const root = ReactDOM.createRoot(document.getElementById('root'));
root.render( );
import React, { useState } from "react";
import ReactDOM from "react-dom/client";
function FavoriteColor() {
const [color, setColor] = useState("red");
return (
<>
<h1>My favorite color is {color}!</h1>
<button
type="button"
onClick={() => setColor("blue")}
>Blue
>
);
}
const root = ReactDOM.createRoot(document.getElementById('root'));
root.render( );
import { useState } from "react";
import { useState } from "react";
function FavoriteColor() {
const [color, setColor] = useState("");
}
import { useState, useEffect } from "react";
import ReactDOM from "react-dom/client";
function Timer() {
const [count, setCount] = useState(0);
useEffect(() => {
setTimeout(() => {
setCount((count) => count + 1);
}, 1000);
});
return <h1>I've rendered {count} times!</h1>;
}
const root = ReactDOM.createRoot(document.getElementById('root'));
root.render(<Timer />);
useEffect(() => {
//Runs on every render
});
useEffect(() => {
//Runs only on the first render
}, []);
useEffect(() => {
//Runs on the first render
//And any time any dependency value changes
}, [prop, state]);
import { useState, useEffect } from "react";
import ReactDOM from "react-dom/client";
function Timer() {
const [count, setCount] = useState(0);
useEffect(() => {
setTimeout(() => {
setCount((count) => count + 1);
}, 1000);
}, []); // >- add empty brackets here
return <h1>'ve rendered {count} times!</h1>;
}
const root = ReactDOM.createRoot(document.getElementById('root'));
root.render(<Timer />);
import { useState, useEffect } from "react";
import ReactDOM from "react-dom/client";
function Counter() {
const [count, setCount] = useState(0);
const [calculation, setCalculation] = useState(0);
useEffect(() => {
setCalculation(() => count * 2);
}, [count]); // <- add the count variable here
return (
<>
<p>Count: {count}</p>
<button onClick={() => setCount((c) => c + 1)}>+</button>
<p>Calculation: {calculation}</p>
</>
);
}
const root = ReactDOM.createRoot(document.getElementById('root'));
root.render(<Counter />);
import { useState } from "react";
import ReactDOM from "react-dom/client";
function Component1() {
const [user, setUser] = useState("Jesse Hall");
return (
<>
<h1>h1>{`Hello ${user}!`}</h1>
<Component2 user={user} />
</>
);
}
function Component2({ user }) {
return (
<>
<h1>Component 2</h1>
<Component3 user={user} />
</>
);
}
function Component3({ user }) {
return (
<>
<h1>Component 3</h1>
<Component4 user={user} />
</>
);
}
function Component4({ user }) {
return (
<>
<h1>Component 4</h1>
<Component5 user={user} />
</>
);
}
function Component5({ user }) {
return (
<>
<h1>Component 5</h1>
<h2>{`Hello ${user} again!`}</h2>
</>
);
}
const root = ReactDOM.createRoot(document.getElementById('root'));
root.render(<Component1 />);
import { useState, createContext } from "react";
import ReactDOM from "react-dom/client";
const UserContext = createContext()