silphius/app/hooks/use-packet.js
2024-06-23 07:35:56 -05:00

17 lines
462 B
JavaScript

import {useContext, useEffect} from 'react';
import ClientContext from '@/context/client.js';
export default function usePacket(type, fn, dependencies) {
const client = useContext(ClientContext);
useEffect(() => {
if (!client) {
return;
}
client.addPacketListener(type, fn);
return () => {
client.removePacketListener(type, fn);
};
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [client, ...dependencies]);
}