react-use-websocket
react-use-websocket copied to clipboard
useWebSocket rendering
i save my data in other js file, so i don't want rerender. how should i do? thanks
const [, setMarketsAtom] = useRecoilState(marketsAtom) // from recoil store ts file
const [marketSelection, setMarketSelection] = useRecoilState(marketSelectionAtom)
const { sendJsonMessage, lastJsonMessage, readyState } = useWebSocket('wss://stream.binance.com:9443/stream', {
share: true,
filter: () => false,
})
useEffect(() => {
...
sendJsonMessage({ id: 1, method: 'SUBSCRIBE', params })
}, [markets])
console.log('useMarketDataSocket: render') // always render
I am facing the same problem, we need an option to stop the re-rendering, sometimes it could be problematic for component life circle management
if use heartbeat, and always render, maybe should use filter
like this:
const { sendJsonMessage, lastMessage, readyState } = useWebSocketIO(socketUrl, {
queryParams: { token },
onMessage: patchMessage,
shouldReconnect: () => mounted.current === false,
reconnectAttempts: 5,
reconnectInterval: 1000 * 5,
filter: event => event.data !== '"pong"',
heartbeat: {
message: 'ping',
returnMessage: 'pong',
timeout: 1000 * 60 * 5,
interval: 1000 * 5,
},
});