17 lines
462 B
JavaScript
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]);
|
|
} |