-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreactjs-gmodapi.tsx
More file actions
37 lines (30 loc) · 1018 Bytes
/
reactjs-gmodapi.tsx
File metadata and controls
37 lines (30 loc) · 1018 Bytes
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
import { useEffect, useState } from "react";
import { listen, unlisten, getUsername, getHealth } from "@teammeadows/atomicapi";
interface HPChangeEvent {
health: number
}
export function UserProfile() {
const [username, setUsername] = useState("");
const [health, setHealth] = useState(0);
// async function that will be fetch username from garry's mod
const fetchUsername = async () => {
// call garry's mod function "getUsername"
setUsername(await getUsername());
// call garry's mod function "getHealth"
setHealth(await getHealth());
}
useEffect(() => {
// add garry's mod event "hpChange" listener
const hpListener = listen<HPChangeEvent>("onHealthChange", event => setHealth(event.health));
// start async fetching
fetchUsername();
// remove "hpChange" listener on component unmount
return () => unlisten(hpListener);
}, [])
return (
<div className="flex flex-col">
<span>{username}</span>
<span>HP {health}%</span>
</div>
)
}