1
0

Created the GroupContext.jsx

This commit is contained in:
Mathias Wagner 2023-11-18 19:32:30 +01:00
parent 71777a2030
commit 447a373271
Signed by: Mathias
GPG Key ID: B8DC354B0A1F5B44

View File

@ -0,0 +1,34 @@
import {createContext, useEffect, useState} from "react";
import {socket} from "@/common/utils/socket.js";
export const GroupContext = createContext({});
export const GroupProvider = ({children}) => {
const [groups, setGroups] = useState([]);
const handleJoin = (group) => {
console.log(group)
setGroups(groups => [...groups, group]);
}
const handleLeave = (group) => {
setGroups(groups => groups.filter(g => g.id !== group.id));
}
useEffect(() => {
socket.on("JOINED", handleJoin);
socket.on("LEFT", handleLeave);
return () => {
socket.off("JOINED", handleJoin);
socket.off("LEFT", handleLeave);
}
}, []);
return (
<GroupContext.Provider value={{groups}}>
{children}
</GroupContext.Provider>
)
}