I'm looking for a practical way to maintain a websocket connection in state with Recoil.
My idea is to put the websocket into an atom:
import useWebSocket from 'react-use-websocket';
import { atom, selector } from 'recoil';
const socketUrl =
'wss://xxx.com';
const webSocket = useWebSocket(socketUrl, {
onOpen: () => console.log('opened'),
//Will attempt to reconnect on all close events, such as server shutting down
shouldReconnect: (closeEvent) => true,
});
// If the page has been archived before, the id in the archive
export const webSocketAtom = atom({
key: 'webSocket',
default: webSocket,
});
Is it a good approach or does I break some rules there?