Listening
useListener
hooks into Socket client and start listening to given event when a component is mounted. It uses
dependency tracking to limit re-rendering and help with performance.
Under the hood, communication with the core systems is established by event emitters. There are many "helper hooks"
that get returned from the hook, like onEvent
, onError
, and onReconnecting
(among others). They will help you
handle various events in the lifecycle of sockets communication.
We used this approach to avoid overloading the base hook with callback logic, which causes low code readability and increases complexity.
import { useListener } from "@hyper-fetch/react";
import { onChatMessage } from "server";
const UsersListPage: React.FC = () => {
const [messages, setMessages] = useState([])
const { data, connected, connecting, onEvent, onError, onReconnecting } = useListener(onChatMessage);
onEvent(({ data: message }) => {
setMessages((prev) => [...prev, message]); // [ MessageType, MessageType, MessageType ]
});
onError((error) => {
console.log(error); // Error Event
});
onReconnecting((reconnectingAttempt) => {
console.log(reconnectingAttempt); // 1
})
if(error && !connecting) {
return <Alert severity="error">{error.message}</Alert>
}
return (
<div>
{connecting && <Loader>}
{!messages.length && <div>No messages</div>}
{!messages.length && <div>{messages.map(message => <div>{message}</div>)}</div>}
</div>
);
};