mirror of
https://github.com/sergeyyarkov/educt-client.git
synced 2026-10-11 10:19:22 +03:00
19 lines
409 B
TypeScript
19 lines
409 B
TypeScript
import { useContext, useEffect } from 'react';
|
|
import { SocketContext } from '@educt/contexts';
|
|
|
|
export const useSocketEvent = (event: string, callback: () => void) => {
|
|
const { socket } = useContext(SocketContext);
|
|
|
|
useEffect(() => {
|
|
if (!socket) {
|
|
return;
|
|
}
|
|
|
|
socket.on(event, callback);
|
|
|
|
return () => {
|
|
socket.off(event, callback);
|
|
};
|
|
}, [callback, event, socket]);
|
|
};
|