const [bidAmount, setBidAmount] = useState<number>(product?.data?.currentPrice);
const { data: session } = useSession();
const socket = useClient();
useEffect(() => {
if (!socket) return;
const handleConnect = () => {
console.log('A user Connected...');
};
const handleBidPlaced = (data: {productId: string,currentPrice: number}) => {
console.log('Broadcasted message from server', data);
if (data.productId === product.data._id) {
setBidAmount(data.currentPrice);
toast.success(`New bid placed: $${data.currentPrice}`);
}
};
const handleBidRejected = (data: any) => {
console.log('Bid rejected message from server', data);
toast.error(data.message);
};
socket.on('connect', handleConnect);
socket.on('bidPlaced', handleBidPlaced);
socket.on('bidRejected', handleBidRejected);
return () => {
socket.off('connect', handleConnect);
socket.off('bidPlaced', handleBidPlaced);
socket.off('bidRejected', handleBidRejected);
};
}, [socket, product?.data?._id]);
The above code ends up giving the Error: Rendered more hooks than during the previous render. I think its due to early return if the socket connection is not eastablished. and if i remove that part it will end up showing socket is null/undefined how could i solve it??