-
Notifications
You must be signed in to change notification settings - Fork 12
/
ManagingInstancesExample.jsx
104 lines (91 loc) · 3.85 KB
/
ManagingInstancesExample.jsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
import { useRef, useEffect, useState } from "react";
export const ManagingInstancesExample = () => {
// Content
const content = {
heading:
"In this ManagingInstancesExamples component, each example demonstrates a different use case of useRef to manage instances and persist values across renders without causing re-renders.",
};
// ------
// ------
// ------
// Example 1: Timer with Pause and Resume
const [time, setTime] = useState(0); // State to hold and display the time
const [isRunning, setIsRunning] = useState(false); // State to toggle between running and paused
const intervalRef = useRef(); // Ref to hold the interval instance
useEffect(() => {
if (isRunning) {
// Starting the interval when isRunning is true
intervalRef.current = setInterval(() => {
setTime((prevTime) => prevTime + 1); // Updating the time every second
}, 1000);
} else {
// Clearing the interval when isRunning is false
clearInterval(intervalRef.current);
}
// Cleanup: Clearing the interval when the component is unmounted
return () => clearInterval(intervalRef.current);
}, [isRunning]); // Dependency array: Effect will run when isRunning changes
// ------
// ------
// ------
// Example 2: Mouse Coordinates Tracker
const [coordinates, setCoordinates] = useState({ x: 0, y: 0 }); // State to hold and display the current mouse coordinates
const [lastCoordinates, setLastCoordinates] = useState({ x: 0, y: 0 }); // State to hold and display the last mouse coordinates after stopping
const timeoutRef = useRef(); // Ref to hold the timeout instance
const handleMouseMove = (e) => {
// Updating the coordinates state with the current mouse position
setCoordinates({ x: e.clientX, y: e.clientY });
// Clearing the previous timeout
clearTimeout(timeoutRef.current);
// Setting a new timeout to update lastCoordinates after 1 second of inactivity
timeoutRef.current = setTimeout(() => {
setLastCoordinates(coordinates);
}, 1000);
};
useEffect(() => {
// Adding an event listener for mousemove to the window
window.addEventListener("mousemove", handleMouseMove);
// Cleanup: Removing the event listener when the component is unmounted
return () => window.removeEventListener("mousemove", handleMouseMove);
}, [coordinates]); // Dependency array: Effect will run when coordinates change
// ------
// ------
// ------
// Example 3: Form Input with Delayed Submit
const [inputValue, setInputValue] = useState(""); // State to hold and display the input value
const submitTimeoutRef = useRef(); // Ref to hold the submit timeout instance
const handleChange = (e) => {
// Updating the inputValue state with the current input
setInputValue(e.target.value);
// Clearing the previous timeout
clearTimeout(submitTimeoutRef.current);
// Setting a new timeout to simulate a submit after 2 seconds of inactivity
submitTimeoutRef.current = setTimeout(() => {
console.log("Submitted:", inputValue); // Logging the submitted value
}, 2000);
};
// ------
// ------
// ------
// Rendering the UI elements for each example
return (
<div>
<h5>{content.heading}</h5>
{/* UI for Example 1 */}
<p>Elapsed time: {time} seconds</p>
<button onClick={() => setIsRunning(!isRunning)}>
{isRunning ? "Pause" : "Resume"} Timer
</button>
{/* UI for Example 2 */}
<p>Current coordinates: {`(${coordinates.x}, ${coordinates.y})`}</p>
<p>Click on the screen to see coordinates change</p>
<p>
Last coordinates after stop:{" "}
{`(${lastCoordinates.x}, ${lastCoordinates.y})`}
</p>
{/* UI for Example 3 */}
<input type="text" value={inputValue} onChange={handleChange} />
<p>Type something to submit after 2 seconds of inactivity</p>
</div>
);
};