Mern Day 2of15
Mern Day 2of15
Mern Day 2of15
• Functional Components
• Class based Components
function welcome() {
Props
• Props(short for properties) are a way to pass data from the
parent component to the child component.
• Props are like function arguments, you can use them as
attributes in components.
function Header(props){
return(<div>
<h1 >{props.text}</h1>
</div>);
}
export default Header;
<Header text="Hello"/>
<Header text="Hello 2"/>
Passing information from one component to another
function Header(){
return(
<div>
<NavBar name="home"/>
<NavBar name="about"/>
</div>);
}
function NavBar(props){
return(
<div>
<h3>{props.name}</h3>
</div>);
}
<React.StrictMode>
<Header/>
</React.StrictMode>
ReactJS State
The State is a way to store and manage the information or data
while creating a React Application. The state is a JavaScript
object that contains the real-time data or information on the
webpage.
function ChangeName(){
Change(Name+1)
}
return(
<div>
<h1>Name: {array[Name]}</h1>
<button onClick={ChangeName}>Click me</button>
</div>
)
}
VirtualRoot.render(
<div>
<Component/>
</div>
)
Implement state using react hooks Example:
return(
<div><h1>Count : {count}</h1>
<button onClick={()=>setCount((count)=>count+1)}>Increase</
button></div>
)
}
React Hooks
In React, Hooks are reusable functions that provide access to
state in React Applications. allow developers to hook into the
state and other React features without having to write a class.
• State Hooks
• Context Hooks
• Ref Hooks
• Effect Hooks
• Performance Hooks
• Resource Hooks
const initialValue =0
const reducer =(state,action)=>{
switch(action){
case "add":
return state+1;
case "sub":
return state-1;
case "clear":
return 0;
default:
return "error";
}
}
const MyComponent = ()=>{
const [count,action]= useReducer(reducer,initialValue);
return(
<div>
<h2>{count}</h2>
<button onClick={()=>action("add")}>Add</button>
<button onClick={()=>action("sub")}>sub</button>
<button onClick={()=>action("clear")}>Clear</button>
</div>)
}
const HookCounterOne=()=> {
const [count, setCount] = useState(0);
//useEffect(<FUNCTION>, <DEPENDECY>)
useEffect(
() => {document.title = `You clicked ${count} times`;}
,[count]);
return (
<div>
<button onClick={() =>
setCount((count) => count + 1)}>
Click {count} times
</button>
</div>
);
}
React Router
React Router, is your essential tool for building single-page
applications (SPAs). Imagine users effortlessly transitioning
between sections, experiencing your website like a fluid app
and React Router makes it possible, paving the way for a
delightful user experience and a modern web presence. A React
website shouldn’t mean a Large page reloads every time users
navigate.
Example
Index.js
import React, { useEffect, useReducer, useState } from 'react';
import ReactDOM from 'react-dom/client';
import Header from './components/Header';
import Login from './components/Login'
import {BrowserRouter as Router,
Routes,
Link,
Route} from 'react-router-dom'
import Register from './components/Register';
const root =document.getElementById('root')//root id from
index.html
const VirtualRoot = ReactDOM.createRoot(root);
<Router>
<div>
<Header/>
</div>
<Routes>
<Route path="/login" element={<Login/>} />
<Route path="/register" element={<Register/>} />
</Routes>
</Router>
)
Header.js
import '../style.css'
import {Link} from 'react-router-dom'
export default function Header(){
return(
<div class="header">
<h3>FlipKart</h3>
<ul>
<li>
<Link to="/">Home</Link>
</li>
<li><Link to="/login">Login</Link></li>
<li><Link to="/register">Register</Link></li>
</ul>
</div>
)
}