Beast of an article! Some great stuff in here.
With this bit at the end -
export const RealTimeItem = () => { const [connections, setConnections] = useState(0); getConnections((conns) => { console.log(conns); setConnections(conns); }); return ( <p> {connections} </p> ); };
If I've read it correctly, I'd suggest wrapping that getConnections call in a useEffect hook (with empty dependencies array) otherwise it will re run every time React goes through a render cycle and resubscribe to the socket event each time.
so -
export const RealTimeItem = () => { const [connections, setConnections] = useState(0); useEffect(() => { getConnections((conns) => { console.log(conns); setConnections(conns); }); return () => { // unsubscribe from socket here }; }, []); return ( <p> {connections} </p> ); };
Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment's permalink.
Hide child comments as well
Confirm
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
Beast of an article! Some great stuff in here.
With this bit at the end -
If I've read it correctly, I'd suggest wrapping that getConnections call in a useEffect hook (with empty dependencies array) otherwise it will re run every time React goes through a render cycle and resubscribe to the socket event each time.
so -