import React, { useState } from 'react';
const Toggle = () => { const [isToggled, setIsToggled] = useState(false);
return (
export default Toggle;
import React, { useState, useEffect } from "react";
function App() { const [time, settime] = useState(0); const [start, setstart] = useState(false); useEffect(() => { let interval = null; if (start) { interval = setInterval(() => { settime((prevTime) => prevTime + 10); }, 10); } else { clearInterval(interval); }
return () => clearInterval(interval);
}, [start]);
return (
{("0" + Math.floor((time / 60000) % 60)).slice(-2)}: {("0" + Math.floor((time / 1000) % 60)).slice(-2)}: {("0" + ((time / 10) % 1000)).slice(-2)}
<div>
<button onClick={() => setstart(true)}>Start</button>
<button onClick={() => setstart(false)}>Stop</button>
<button
onClick={() => {
settime(0);
setstart(false);
}}
>
Reset
</button>
</div>
</div>
); }
export default App;
import React,{useState,useEffect} from 'react'
function App() { const [count,setcount]=useState(0);
useEffect(()=>{ const timer=setTimeout(() => { setInterval(()=>{ setcount((prevcount)=>prevcount+1) },1000)
}, 1000);
const cleanup=()=>{ clearTimeout(timer) } return cleanup;
},[])
return (
</div>
) }
export default App
import React, { useState } from 'react';
const App = ({ children, open, onClose }) => { const [isOpen, setIsOpen] = useState(open);
const handleClose = () => { setIsOpen(false); onClose(); };
if (!isOpen) { return null; }
return ( <div style={{ backgroundColor: 'white', position: 'fixed', top: '50%', left: '50%', transform: 'translate(-50%, -50%)', padding: '20px', border: '1px solid black' }}> <button onClick={handleClose} style={{ float: 'right' }}> ); };
export default App;
import React, { useState, useEffect } from 'react';
const Palindrome = () => { const [palindromes, setPalindromes] = useState([]);
useEffect(() => { let palindromeArray = []; for (let i = 1; i <= 1000; i++) { let number = i; let reverse = 0; let remainder;
while (number > 0) {
remainder = number % 10;
reverse = reverse * 10 + remainder;
number = Math.floor(number / 10);
}
if (reverse === i) {
palindromeArray.push(i);
}
}
setPalindromes(palindromeArray);
}, []);
return (
); };export default Palindrome;
import React from 'react' import { useState } from 'react'
const RenderAll = () => { const data=[ { "id": "1", "type": "Regular" }, { "id": "2", "type": "Chocolate" }, { "id": "3", "type": "Blueberry" }, { "id": "4", "type": "Devil's Food" } ]
const [text, SetText] = useState(data)
const HandleDelete=(id)=>{
const Del=text.filter((el)=>el.id!==id)
SetText(Del)
}
console.log(text)
return ( <>
{text.map((el)=></> ) }
export default RenderAll
import React, { useState, useEffect } from "react"; import "../../App.css"
const getData = async (url) => { let res = await fetch(url); return res.json(); };
function Fetch() { const [info, setinfo] = useState([]);
useEffect(() => { getData("https://fakestoreapi.com/products?limit=10").then((res) => { setinfo(res); }); }, []); console.log(info);
return ( <>
<div className={"main"}>
{info?.map((el) => (
<div key={el.id}>
<img src={el.image} alt="products" />
<p>{el.title}</p>
<p>{el.category}</p>
<p>{el.price}</p>
</div>
))}
</div>
</>
); }
export default Fetch;
import React,{useRef} from 'react'
function Useref() { const inputRef = useRef(null) const handleClick=()=>{ inputRef.current.focus() inputRef.current.value="100"; inputRef.current.style.color="red"
}
return (
<div>
<h1>Use Ref in react</h1>
</div>
) }
export default Useref
import React, { useState } from "react";
function Input() { const [input, setinput] = useState({}); const [singlevalue, setsinglevalue] = useState(""); const handleChange = (e) => { const { name, value } = e.target; setinput({ ...input, [name]: value, }); };
const handleSUbmit = (e) => { e.preventDefault(); console.log(input); };
// SIngle input box value const handlekro = () => { console.log(singlevalue); };
return (
{/* A single input box */}
<input type="submit" value="Submit" />
</form>
<input
type="text"
placeholder="I am a single input box"
onChange={(e) => setsinglevalue(e.target.value)}
/>
<button onClick={handlekro}>Submit</button>
</div>
); }
export default Input;
import React from 'react'
function Render(){
Render A object const obj={ name:"Amit", age:45, address:"Delhi", profession:"Software Developer", education:"Graduate" } return( <> { Object.entries(obj).map(([key,value])=>( li key={key}> {key}:{value} /li> )) } </> )
} export default Render;
import React,{useReducer,useState} from "react";
const Reducer=(state,action)=>{ switch(action.type){
case "INCREMENT":return state+1
case "DECREMENT":return state-1;
case "RESET":return 0;
case "UPDATE":return state+action.payload;
case "DECRESEBYVAL":return state-action.payload default:return state;
} }
const initialState=0
function UseReducer(){ const [state, dispatch] = useReducer(Reducer,initialState);
const [val,setval]=useState('');
return( <>
<h1>A basic Counter but by using UseReducer hook</h1>
h1>{state}h1 button onClick={()=>dispatch({type:"INCREMENT"})}>INC button button onClick={()=>dispatch({type:"RESET"})}>RESET button button onClick={()=>dispatch({type:"DECREMENT"})} disabled={state<1}>DEC button
INcrease The counter by that Value of input box input type="text" placeholder="Enter value here" onChange={(e)=>setval(Number(e.target.value))}/>
button onClick={()=>dispatch({type:"UPDATE",payload:val})}>INCBYVAL button button onClick={()=>dispatch({type:"DECRESEBYVAL",payload:val})}>DECBYVAL button
</button>
)
}
export default UseReducer;