Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

283
Visualizações
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...

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

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.

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda