我正在寻找一种实用的方法来保持与 Recoil 的 websocket 连接。
我的想法是将websocket放入一个原子中:
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,
});
这是一个好方法还是我在那里打破了一些规则?