React
ReactJS is a simple, feature rich, component based JavaScript UI library. It can be used to develop small applications as well as big, complex applications. ReactJS provides minimal and solid feature set to kick-start a web application. React community compliments React library by providing large set of ready-made components to develop web application in a record time. React community also provides advanced concept like state management, routing, etc., on top of the React library.
Features
The salient features of React library are as follows :
- Solid base architecture
- Extensible architecture
- Component based library
- JSX based design architecture
- Declarative UI library
- Benefits
Benefits
- Easy to learn
- Easy to adept in modern as well as legacy application
- Faster way to code a functionality
- Availability of large number of ready-made component
- Large and active community
Applications
Few popular websites powered by React library are listed below −
- Facebook, popular social media application
- Instagram, popular photo sharing application
- Netflix, popular media streaming application
- Code Academy, popular online training application
- Reddit, popular content sharing application
First Program
/myReactApp/src/App.js:
function App() {
return (
<div className="App">
<h1>Hello World!</h1>
</div>
);
}
export default App;
Setting up
npx create-react-app my-react-app
npm start
or
git clone {the url to the GitHub repo}
npm install
npm start
Build Website
https://www.youtube.com/watch?v=QwarZBtFoFA
.
├─ node_modules
├─ public
│ └─ Index.html
├─src
│ ├─ assets
│ │ └─ images.jpg
│ ├─ components
│ │ ├─ Footer.js
│ │ ├─ MenuItem.js
│ │ └─ Navbar.js
│ ├─ helpers
│ │ └─ MenuList.js
│ ├─ pages
│ │ ├─ About.js
│ │ ├─ Contact.js
│ │ ├─ Home.js
│ │ └─ Menu.js
│ ├─ styles
│ │ ├─ About.css
│ │ ├─ Contact.css
│ │ ├─ Footer.css
│ │ ├─ Home.css
│ │ ├─ Menu.css
│ │ └─ Navbar.css
│ ├─ App.css
│ ├─ App.js
│ └─ index.js
├─ package.json
└─ README.md
App.js
import "./App.css";
import Navbar from "./components/Navbar";
import Footer from "./components/Footer";
import Home from "./pages/Home";
import Menu from "./pages/Menu";
import About from "./pages/About";
import Contact from "./pages/Contact";
import { BrowserRouter as Router, Route, Routes } from "react-router-dom";
function App() {
return (
<div className="App">
<Router>
<Navbar />
<Routes>
<Route path="/" element={<Home/>} />
<Route path="/menu" element={<Menu/>} />
<Route path="/about" element={<About/>} />
<Route path="/contact" element={<Contact/>} />
</Routes>
<Footer />
</Router>
</div>
);
}
export default App;
index.js
import React from "react";
import ReactDOM from "react-dom";
import App from "./App";
import reportWebVitals from "./reportWebVitals";
ReactDOM.render(
<React.StrictMode>
<App />
</React.StrictMode>,
document.getElementById("root")
);
App.css
.App {
width: 100%;
height: auto;
display: flex;
flex-direction: column;
font-family: Arial, Helvetica, sans-serif;
}
body {
padding: 0;
margin: 0;
}
components\Navbar.js
import React, { useState } from "react";
import Logo from "../assets/pizzaLogo.png";
import { Link } from "react-router-dom";
import ReorderIcon from "@material-ui/icons/Reorder";
import "../styles/Navbar.css";
function Navbar() {
const [openLinks, setOpenLinks] = useState(false);
const toggleNavbar = () => {
setOpenLinks(!openLinks);
};
return (
<div className="navbar">
<div className="leftSide" id={openLinks ? "open" : "close"}>
<img src={Logo} />
<div className="hiddenLinks">
<Link to="/"> Home </Link>
<Link to="/menu"> Menu </Link>
<Link to="/about"> About </Link>
<Link to="/contact"> Contact </Link>
</div>
</div>
<div className="rightSide">
<Link to="/"> Home </Link>
<Link to="/menu"> Menu </Link>
<Link to="/about"> About </Link>
<Link to="/contact"> Contact </Link>
<button onClick={toggleNavbar}>
<ReorderIcon />
</button>
</div>
</div>
);
}
export default Navbar;
pages\Home.js
import React from "react";
import { Link } from "react-router-dom";
import BannerImage from "../assets/pizza.jpeg";
import "../styles/Home.css";
function Home() {
return (
<div className="home" style={{ backgroundImage: `url(${BannerImage})` }}>
<div className="headerContainer">
<h1> Pedro's Pizzeria </h1>
<p> PIZZA TO FIT ANY TASTE</p>
<Link to="/menu">
<button> ORDER NOW </button>
</Link>
</div>
</div>
);
}
export default Home;
components\Footer.js
import React from "react";
import InstagramIcon from "@material-ui/icons/Instagram";
import TwitterIcon from "@material-ui/icons/Twitter";
import FacebookIcon from "@material-ui/icons/Facebook";
import LinkedInIcon from "@material-ui/icons/LinkedIn";
import "../styles/Footer.css";
function Footer() {
return (
<div className="footer">
<div className="socialMedia">
<InstagramIcon /> <TwitterIcon /> <FacebookIcon /> <LinkedInIcon />
</div>
<p> © 2021 pedrotechpizza.com</p>
</div>
);
}
export default Footer;
pages\Menu.js
import React from "react";
import { MenuList } from "../helpers/MenuList";
import MenuItem from "../components/MenuItem";
import "../styles/Menu.css";
function Menu() {
return (
<div className="menu">
<h1 className="menuTitle">Our Menu</h1>
<div className="menuList">
{MenuList.map((menuItem, key) => {
return (
<MenuItem
key={key}
image={menuItem.image}
name={menuItem.name}
price={menuItem.price}
/>
);
})}
</div>
</div>
);
}
export default Menu;
components\MenuItem.js
import React from "react";
function MenuItem({ image, name, price }) {
return (
<div className="menuItem">
<div style={{ backgroundImage: `url(${image})` }}> </div>
<h1> {name} </h1>
<p> ${price} </p>
</div>
);
}
export default MenuItem;
helpers\MenuList.js
import Pepperoni from "../assets/pepperoni.jpg";
import Margherita from "../assets/margherita.jpg";
import PedroTechSpecial from "../assets/pedrotechspecial.jpg";
import Vegan from "../assets/vegan.jpg";
import Pineapple from "../assets/pineapple.jpg";
import Expensive from "../assets/expensive.jpg";
export const MenuList = [
{
name: "Pepperoni Pizza",
image: Pepperoni,
price: 15.99,
},
{
name: "Margherita Pizza",
image: Margherita,
price: 11.99,
},
{
name: "PedroTech Special Pizza",
image: PedroTechSpecial,
price: 256.53,
},
{
name: "Vegan Pizza",
image: Vegan,
price: 17.99,
},
{
name: "Pineapple Pizza",
image: Pineapple,
price: 4.99,
},
{
name: "Very Expensive Pizza",
image: Expensive,
price: 1997.99,
},
];
pages\About.js
import React from "react";
import MultiplePizzas from "../assets/multiplePizzas.jpeg";
import "../styles/About.css";
function About() {
return (
<div className="about">
<div
className="aboutTop"
style={{ backgroundImage: `url(${MultiplePizzas})` }}
></div>
<div className="aboutBottom">
<h1> ABOUT US</h1>
<p>
Lorem ipsum dolor sit amet consectetur adipisicing elit. Maxime
mollitia, molestiae quas vel sint commodi repudiandae consequuntur
voluptatum laborum numquam blanditiis harum quisquam eius sed odit
fugiat iusto fuga praesentium optio, eaque rerum! Provident similique
accusantium nemo autem. Veritatis obcaecati tenetur iure eius earum ut
molestias architecto voluptate aliquam nihil, eveniet aliquid culpa
officia aut! Impedit sit sunt quaerat, odit, tenetur error, harum
nesciunt ipsum debitis quas aliquid. Reprehenderit, quia. Quo neque
error repudiandae fuga? Ipsa laudantium molestias eos sapiente
officiis modi at sunt excepturi expedita sint? Sed quibusdam
recusandae alias error harum maxime adipisci amet laborum.
Perspiciatis minima nesciunt dolorem! Officiis iure rerum voluptates a
cumque velit
</p>
</div>
</div>
);
}
export default About;
pages\Contact.js
import React from "react";
import PizzaLeft from "../assets/pizzaLeft.jpg";
import "../styles/Contact.css";
function Contact() {
return (
<div className="contact">
<div
className="leftSide"
style={{ backgroundImage: `url(${PizzaLeft})` }}
></div>
<div className="rightSide">
<h1> Contact Us</h1>
<form id="contact-form" method="POST">
<label htmlFor="name">Full Name</label>
<input name="name" placeholder="Enter full name..." type="text" />
<label htmlFor="email">Email</label>
<input name="email" placeholder="Enter email..." type="email" />
<label htmlFor="message">Message</label>
<textarea
rows="6"
placeholder="Enter message..."
name="message"
required
></textarea>
<button type="submit"> Send Message</button>
</form>
</div>
</div>
);
}
export default Contact;
styles\Home.css
.home {
width: 100%;
height: 90vh;
display: flex;
justify-content: center;
align-items: left;
flex-direction: column;
background-position: center;
background-repeat: no-repeat;
background-size: cover;
font-family: "Gill Sans", "Gill Sans MT", Calibri, "Trebuchet MS", sans-serif;
}
.headerContainer {
width: auto;
margin-left: 50px;
}
.headerContainer h1 {
justify-content: center;
align-items: center;
font-size: 90px;
height: 60px;
font-weight: 50;
color: black;
}
.headerContainer p {
padding: 0;
font-size: 40px;
font-weight: lighter;
color: black;
}
.headerContainer button {
color: black;
border: none;
border-radius: 5px;
cursor: pointer;
justify-content: center;
background-color: #121619;
height: 50px;
width: 180px;
color: white;
text-decoration: none;
font-size: 20px;
}
.headerContainer button:hover {
background-color: #07090a;
cursor: pointer;
transition: 0.3s ease-in;
}
@media only screen and (max-width: 650px) {
.home {
justify-content: center;
align-items: center;
}
.headerContainer h1,
.headerContainer p {
}
.headerContainer {
margin-left: 0px;
border-radius: 10px;
padding: 10px;
width: 80%;
display: flex;
justify-content: center;
align-items: center;
flex-direction: column;
background-color: #121619;
color: white;
}
.headerContainer h1 {
font-size: 40px;
height: 30px;
color: white;
}
.headerContainer p {
font-size: 30px;
color: white;
}
.headerContainer button {
background-color: white;
color: #121619;
margin-bottom: 30px;
}
.headerContainer button:hover {
background-color: rgb(225, 225, 225);
color: #121619;
}
}
styles/Navbar.css
.navbar {
width: 100%;
height: 100px;
background-color: #121619;
display: flex;
flex-direction: row;
}
.navbar .leftSide {
flex: 50%;
height: 100%;
display: flex;
align-items: center;
padding-left: 150px;
}
.navbar .leftSide img {
width: 70px;
}
.navbar .rightSide {
flex: 50%;
height: 100%;
display: flex;
align-items: center;
justify-content: center;
}
.navbar a {
color: white;
text-decoration: none;
margin: 20px;
}
.navbar .rightSide button {
background-color: transparent;
border: none;
color: white;
cursor: pointer;
}
.navbar .rightSide svg {
font-size: 40px;
}
.navbar #open {
padding-left: 0px;
}
.navbar #open img {
display: none;
}
.navbar #close img {
display: inherit;
}
.navbar #open .hiddenLinks {
display: inherit;
margin-left: 30px;
}
.navbar #close .hiddenLinks {
display: none;
}
.navbar #open a {
width: 70px;
margin: 5px;
}
@media only screen and (max-width: 900px) {
.navbar .rightSide a {
width: 70px;
}
.navbar .leftSide {
padding-left: 50px;
}
}
@media only screen and (max-width: 600px) {
.navbar .rightSide a {
display: none;
}
.navbar .rightSide {
justify-content: flex-end;
padding-right: 50px;
}
.navbar .rightSide button {
display: inherit;
}
}
@media only screen and (min-width: 600px) {
.navbar .rightSide button {
display: none;
}
.hiddenLinks {
display: none;
}
}
styles\Footer.css
.footer {
width: 100%;
height: 200px;
background-color: #121619;
display: flex;
justify-content: center;
align-items: center;
flex-direction: column;
padding-top: 10px;
}
.socialMedia svg {
color: white;
margin: 20px;
font-size: 70px;
cursor: pointer;
}
.footer p {
color: white;
}
styles\Menu.css
.menu {
width: 100%;
height: auto;
display: flex;
justify-content: center;
align-items: center;
flex-direction: column;
}
.menu .menuTitle {
font-family: cursive;
font-size: 60px;
}
.menuList {
width: 70vw;
height: auto;
display: grid;
grid-template-columns: 1fr 1fr 1fr;
place-items: center;
}
/* MENU ITEM STYLING */
.menuItem {
border-radius: 15px;
width: 300px;
height: 350px;
margin: 20px;
box-shadow: 0px 3px 15px rgba(0, 0, 0, 0.2);
}
.menuItem:hover {
box-shadow: 0px 3px 15px rgba(0, 0, 0, 0.5);
transition: 0.3s ease-in;
cursor: pointer;
}
.menuItem div {
border-top-left-radius: 15px;
border-top-right-radius: 15px;
width: 100%;
height: 200px;
background-position: center;
background-repeat: no-repeat;
background-size: cover;
}
.menuItem h1,
.menuItem p {
margin-left: 20px;
}
@media only screen and (max-width: 1300px) {
.menuList {
grid-template-columns: 1fr 1fr;
}
}
@media only screen and (max-width: 800px) {
.menuList {
grid-template-columns: 1fr;
}
}
styles\About.css
.about {
width: 100%;
height: 90vh;
display: flex;
flex-direction: column;
}
.aboutTop {
width: 100%;
height: 45%;
background-position: center;
background-repeat: no-repeat;
background-size: cover;
font-family: "Gill Sans", "Gill Sans MT", Calibri, "Trebuchet MS", sans-serif;
}
.aboutBottom {
display: flex;
flex-direction: column;
height: 55%;
align-items: center;
margin-top: 50px;
}
.aboutBottom p {
width: 1200px;
}
.about .aboutBottom h1 {
font-weight: 400;
font-size: 70px;
color: black;
height: 30px;
display: flex;
justify-content: center;
transform: translateY(-40px);
}
styles\Contact.css
.contact {
width: 100%;
height: 90vh;
display: flex;
}
.contact .leftSide {
height: 100%;
flex: 50%;
background-position: center;
background-repeat: no-repeat;
background-size: cover;
}
.contact .rightSide {
height: 100%;
flex: 50%;
display: flex;
flex-direction: column;
justify-content: center;
}
.contact .rightSide h1 {
font-family: "Trebuchet MS", "Lucida Sans Unicode", "Lucida Grande",
"Lucida Sans", Arial, sans-serif;
font-weight: 10;
font-size: 50px;
margin-left: 30px;
}
form {
display: flex;
flex-direction: column;
width: auto;
height: auto;
padding: 30px;
}
form input {
height: 40px;
width: 80%;
border: none;
border-bottom: 1px solid #121619;
color: black;
}
form textarea {
margin-top: 15px;
height: 70px;
width: 80%;
border: none;
border-bottom: 1px solid #121619;
color: black;
}
input::placeholder,
textarea::placeholder {
font-weight: bold;
color: black;
font-family: Arial, Helvetica, sans-serif;
}
form input:focus,
textarea:focus {
outline: none;
}
form label {
margin-top: 15px;
color: grey;
}
form button {
margin-top: 40px;
width: 140px;
height: 40px;
border: none;
background-color: #121619;
color: whitesmoke;
font-size: 15px;
cursor: pointer;
}
Rest API
HTTP GET Request
https://www.youtube.com/watch?v=qXvFaEkkZH8
npm install axios
https://jsonplaceholder.typicode.com/posts
App.js
import './App.css';
import PostList from './API/PostList'
import PostForm from './API/PostForm'
function App() {
return (
<div className="App">
{/* <PostList /> */}
<PostForm />
</div>
);
}
export default App;
PostList.js
import React, { Component } from 'react'
import axios from 'axios'
export default class PostList extends Component {
constructor(props) {
super(props)
this.state = {
posts: []
}
}
componentDidMount(){
axios.get('https://jsonplaceholder.typicode.com/posts')
.then(response =>{
this.setState({
posts: response.data
})
console.log(response.data)
})
}
render() {
const {posts} = this.state
return (
<div>
<h1>List of Posts</h1>
{
posts.map(post => <div key={post.id}>{post.title}</div>)
}
</div>
)
}
}
HTTP POST Request
PostForm.js
import React, { Component } from 'react'
import axios from 'axios'
export default class PostForm extends Component {
constructor(props) {
super(props)
this.state = {
userId: '',
title: '',
body: ''
}
}
handleChange= (e) =>{
this.setState({
[e.target.name]: e.target.value
})
}
handleSubmit = (e) => {
e.preventDefault()
axios.post('https://jsonplaceholder.typicode.com/posts', this.state)
.then(response =>{
console.log(response)
})
}
render() {
const {userId, title, body} = this.state
return (
<div>
<form onSubmit={this.handleSubmit}>
<div>
<label>User Id</label>
<input
type = 'text'
name = 'userId'
value = {'userId'}
onChange= {this.handleChange}>
</input>
</div>
<div>
<label>Title</label>
<input
type = 'text'
name = 'title'
value = {'title'}
onChange= {this.handleChange}>
</input>
</div>
<div>
<label>Body</label>
<input
type = 'text'
name = 'body'
value = {'body'}
onChange= {this.handleChange}>
</input>
</div>
<div>
<button type='submit'>Submit</button>
</div>
</form>
</div>
)
}
}