'Why am i getting " Expected an assignment or function call and instead saw an expression" error here?
Im getting " Expected an assignment or function call and instead saw an expression" error at the seconduse effect, it finds the socket port correctly, am i performing the socket.on wrong? the socket server starts correctly, and i even get my console log in index.js to output "a user is connected"
const [socket, setSocket] = useState(null)
useEffect(() => {
setSocket(io("ws://localhost:8900"))
}, [])
useEffect(() => {
socket?.on("welcome", (message) => {
console.log(message)
})
}, [socket])
index.js for socket
const io = require("socket.io") (8900, {
cors: {
origin: "http://localhost:3000",
}
})
io.on("connection", (socket) =>{
console.log("a user is connected")
io.emit("welcome","hello this is socket server")
})
edit: whats even weirder, is the "hello this is a socket server" actually console logs, then this error still happens... not sure why...
Solution 1:[1]
Figured out my issue, set the socket with socket.current if your using a useEffect in react, if your just doing a const socket , then thats fine. Their will be socket Issues on any refresh or update, causing errors for something trying to call an old socket, using a different socketId.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | gdubb williamson |