Replies: 1 comment 1 reply
-
Hmm... that's a great question. Using the explicit event name seems to work: export const useSocketEvent = <K extends keyof ServerToClientEvents>(type: K) => {
const [result, setResult] = useState<Parameters<ServerToClientEvents[K]>>();
useEffect(() => {
const callback = (...args: Parameters<ServerToClientEvents["ping"]>) => {
setResult(args);
}
socket.on("ping", callback);
return () => {
socket.off("ping", callback);
};
}, [type]);
return result;
}; Not sure why the TypeScript compiler errors with the generic |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
I'm trying to create a typed react hook for stateful events, this may be more of a TypeScript question but it's directly related to the lib, what am I doing wrong here?
when using the
callback
as an argument toon
oroff
, I get the following error:ts(2345)
Argument of type:
is not assignable to parameter of type:
it seems the second argument is expected to be never
Beta Was this translation helpful? Give feedback.
All reactions