Skip to content

Commit aa0ca5c

Browse files
Merge branch 'main' of github.com:snippet-labs/REACTLAB
2 parents 45dddce + a494203 commit aa0ca5c

File tree

12 files changed

+418
-21
lines changed

12 files changed

+418
-21
lines changed

src/App.jsx

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,22 @@
11
// COMPONENTS
22
import Greeting from "./components/Basics/Greeting";
3-
// import CounterEffect from "./components/Hooks/useEffectHook/Counter1";
4-
import Counter from "./components/Hooks/useStateHook/Counter";
3+
// import Counter from "./components/Hooks/useStateHook/Counter";
54
import Form from "./project/Form";
65
import ThemeToggler from "./project/ThemeToggler";
76
import HandlingForm from "./components/Hooks/useRefHook/HandlingForm";
8-
7+
import ChangeTheme from "./components/Hooks/useMemo/ChangeTheme";
8+
import DisplayList from "./components/Hooks/useCallback/DisplayList";
99
// TRY IT YOURSELF COMPONENTS
10-
// import ModifiedCounter from "./tryityourself/tushit/ModifiedCounter";
10+
import FormDetails from "./tryityourself/Akash/FormDetails";
11+
import RandomBackground from "./tryityourself/Akash/RandomBackground";
12+
13+
// PROJECTS
14+
import StopWatch from "./project/StopWatch/StopWatch";
1115

1216
const App = () => {
13-
return <HandlingForm />;
17+
18+
return <DisplayList />;
19+
1420
};
1521

1622
export default App;
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import { useCallback, useState } from "react";
2+
import List from "./List";
3+
4+
const DisplayList = () => {
5+
const [number, setNumber] = useState(1);
6+
const [dark, setDark] = useState(false);
7+
8+
// const getItems = () => {
9+
// return [number, number + 1, number + 2];
10+
// };
11+
12+
const getItems = useCallback(() => {
13+
return [number, number + 1, number + 2];
14+
}, [number]);
15+
16+
const theme = {
17+
backgroundColor: dark ? "black" : "white",
18+
color: dark ? "white" : "black",
19+
};
20+
21+
return (
22+
<div style={theme}>
23+
<input
24+
type="number"
25+
value={number}
26+
onChange={(e) => setNumber(parseInt(e.target.value))}
27+
></input>
28+
<button onClick={() => setDark((prevDark) => !prevDark)}>
29+
Change theme
30+
</button>
31+
<List getItems={getItems} />
32+
</div>
33+
);
34+
};
35+
36+
export default DisplayList;
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { useEffect, useState } from "react";
2+
3+
const List = ({ getItems }) => {
4+
const [items, setItems] = useState([]);
5+
6+
useEffect(() => {
7+
setItems(getItems());
8+
9+
console.log("Updating items");
10+
}, [getItems]);
11+
12+
return items.map((item, index) => <div key={index}>{item}</div>);
13+
};
14+
15+
export default List;
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import { useMemo, useState } from "react";
2+
3+
const ChangeTheme = () => {
4+
const [number, setNumber] = useState(0);
5+
const [dark, setDark] = useState(false);
6+
7+
const doubleNumber = useMemo(() => {
8+
return slowFunction(number);
9+
}, [number]);
10+
11+
const themeStyles = {
12+
backgroundColor: dark ? "black" : "white",
13+
color: dark ? "white" : "black",
14+
};
15+
16+
function slowFunction(num) {
17+
console.log("Calling slow function");
18+
for (let i = 0; i <= 1000000000; i++) {}
19+
return num * 2;
20+
}
21+
22+
return (
23+
<>
24+
<input
25+
type="number"
26+
value={number}
27+
onChange={(e) => setNumber(e.target.value)}
28+
></input>
29+
<button onClick={() => setDark((prevDark) => !prevDark)}>
30+
Change theme
31+
</button>
32+
<div style={themeStyles}>{doubleNumber}</div>
33+
</>
34+
);
35+
};
36+
37+
export default ChangeTheme;
Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,20 @@
1-
import React from 'react'
2-
import { useState } from 'react'
1+
import React from "react";
2+
import { useState } from "react";
33
const Counter = () => {
4-
const [count, setCount] = useState(0);
5-
6-
// let a =0;
7-
// function add (){
8-
// a =+1;
9-
// console.log(a);
10-
// }
11-
console.log("Component Rendered")
4+
const [count, setCount] = useState(0);
5+
// let a =0;
6+
// function add (){
7+
// a =+1;
8+
// console.log(a);
9+
// }
10+
console.log("Component Rendered");
1211
return (
1312
<div>
14-
<button onClick={() => setCount(count +1)}>Add</button>
15-
<h1>{count}</h1>
16-
<button onClick={() => setCount(count -1)}>Subtract</button>
13+
<button onClick={() => setCount(count + 1)}>Add</button>
14+
<h1>{count}</h1>
15+
<button onClick={() => setCount(count - 1)}>Subtract</button>
1716
</div>
18-
)
19-
}
17+
);
18+
};
2019

21-
export default Counter;
20+
export default Counter;

src/index.css

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,5 @@
11
@import "tailwindcss";
22
@import "./components/Hooks/useRefHook/HandlingForm.style.css";
3+
@import "./tryityourself/Akash/FormDetails.style.css";
4+
@import "./tryityourself/Akash/RandomBackground.style.css";
5+
@import "./project/StopWatch/StopWatch.style.css";

src/project/StopWatch/StopWatch.jsx

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
import React, { useEffect, useRef, useState } from "react";
2+
3+
// ICONS
4+
import { FaPlay } from "react-icons/fa";
5+
import { FaPause } from "react-icons/fa6";
6+
import { LuTimerReset } from "react-icons/lu";
7+
8+
const StopWatch = () => {
9+
// STATES
10+
const [isRunning, setIsRuning] = useState(() => {
11+
return false;
12+
});
13+
const [elapsedTime, setElapsedTime] = useState(0);
14+
const startTimeRef = useRef(0);
15+
const intervalTimeRef = useRef(null);
16+
17+
// SIDE-EFFECTS
18+
useEffect(() => {
19+
if (isRunning) {
20+
startTimeRef.current = Date.now() - elapsedTime;
21+
intervalTimeRef.current = setInterval(() => {
22+
setElapsedTime(Date.now() - startTimeRef.current);
23+
}, 10);
24+
25+
return () => clearInterval(intervalTimeRef.current);
26+
}
27+
}, [isRunning]);
28+
29+
// HANDLER FUNCTIONS
30+
const handleStartOrStop = () => {
31+
return setIsRuning((previous) => !previous);
32+
};
33+
34+
const handleReset = () => {
35+
setIsRuning(false);
36+
setElapsedTime(0);
37+
};
38+
39+
const displayStopWatchTime = (elapsedTime) => {
40+
let hours = Math.floor(elapsedTime / (1000 * 60 * 60));
41+
let minutes = Math.floor((elapsedTime / (1000 * 60)) % 60);
42+
let seconds = Math.floor((elapsedTime / 1000) % 60);
43+
let milliseconds = Math.floor((elapsedTime % 1000) / 10);
44+
return `
45+
${hours.toString().padStart(2, "0")}:
46+
${minutes.toString().padStart(2, "0")}:
47+
${seconds.toString().padStart(2, "0")}:
48+
${milliseconds.toString().padStart(2, "0")}`;
49+
};
50+
51+
return (
52+
<div className="CONTAINER">
53+
<div className="text-6xl">{displayStopWatchTime(elapsedTime)}</div>
54+
<div className="mt-3 flex items-center justify-center gap-3">
55+
<button className="BUTTON" onClick={handleStartOrStop}>
56+
{isRunning ? <FaPause size={20} /> : <FaPlay size={20} />}
57+
</button>
58+
<button className="BUTTON" onClick={handleReset}>
59+
<LuTimerReset size={20} />
60+
</button>
61+
</div>
62+
</div>
63+
);
64+
};
65+
66+
export default StopWatch;
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
.CONTAINER {
2+
@apply h-screen w-full flex flex-col items-center justify-center bg-slate-500 text-black;
3+
}
4+
5+
.BUTTON {
6+
@apply p-2 rounded-md border-1 border-black hover:bg-black hover:text-white hover:cursor-pointer;
7+
}

0 commit comments

Comments
 (0)