import { useEffect } from "react"
import {useWebSocket} from 'react-use-websocket'
import throttle from 'lodash.throttle'
export function Home ({userName}){
const WS_URL = 'ws://127.0.0.1:8000'
const {sendJsonMessage} = useWebSocket(WS_URL,{
queryParams : { userName}
})
// const socket = new WebSocket(`ws://localhost:8000?userName=${userName}`)
// socket.addEventListener('open',() => {
// socket.send(JSON.stringify({x:23,y:223}))
// console.log("Connected to server")
// }
// )
// socket.addEventListener('message',event => {
// console.log("The Server data is, ",JSON.parse(event.data))
// })
const sendJsonMessageThrottled = throttle(sendJsonMessage,50)
useEffect(() => {
window.addEventListener('mousemove',(e)=>{
sendJsonMessageThrottled({x:e.clientX, y:e.clientY})
})
},[])
return <h1>Hello {userName}</h1>
}
I get a react error that useWebSocket isnt a function when I call it in this code, passing in the server socket path and queryParams
How can I resolve it