'React axios request on socket.io backend emit
I'd like to use my axios get request "reload", when the server emit the serverchange listener.
The socket is working, I've got the "msg" from the server, and my code is working for a while, but every request is taking more time... The message is a UUID, so it's unique. (if it matters)
I'm pretty new in React even more in socket.io, so probably missing some logical step, I've spent half of my day to figure it out with no luck.
const [messages, setMessages] = useState([])
const [update, setUpdate] = useState(null)
useEffect(() => {
const socket = io(process.env.REACT_APP_SOCKET_ENDPOINT)
const cb = (msg) => setUpdate(msg)
socket.on("serverchange", cb)
return () => {
socket.off("serverchange", cb)
}
}, [])
useEffect(() => {
axios
.get("/api/messages")
.then((res) => setMessages(res.data))
.catch((err) => console.error(err))
}, [update])
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
