-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path13-callback.html
More file actions
55 lines (45 loc) · 1.63 KB
/
Copy path13-callback.html
File metadata and controls
55 lines (45 loc) · 1.63 KB
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
<!-- useCallback para memorizar funções e evitar recriação desnecessária a cada render. -->
<!doctype html>
<html lang="pt-br">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Rocketseat - React.js - Fundamentos</title>
</head>
<body>
<div id="root"></div>
<script src="https://unpkg.com/react@18/umd/react.development.js"></script>
<script src="https://unpkg.com/react-dom@18/umd/react-dom.development.js"></script>
<script src="https://unpkg.com/@babel/standalone/babel.min.js"></script>
<script type="text/babel">
function App() {
const [is12Hours, setIs12Hours] = React.useState(false);
const [time, setTime] = React.useState("");
const updateTime = React.useCallback(() => {
const now = new Date(Date.now());
const formattedTime = now.toLocaleTimeString("pt-BR", {
hour: "2-digit",
minute: "2-digit",
second: "2-digit",
hour12: is12Hours,
});
setTime(formattedTime);
});
React.useEffect(() => {
updateTime();
const timer = setInterval(() => {
updateTime()
}, 1000);
return () => clearInterval(timer);
}, [updateTime]);
return (
<main>
<div>🕒 {time}</div>
<button onClick={() => setIs12Hours(!is12Hours)} >Alterar para formato {is12Hours ? "24h" : "12h"}</button>
</main>
);
}
ReactDOM.createRoot(document.getElementById("root")).render(<App />);
</script>
</body>
</html>