Я новичок в socket.io
и пытаюсь реализовать простое уведомление.
Моя проблема в том, что я выполняю функцию handleClick
, чтобы преобразовать open
в истинное значение и когда socket.on
запустить неверное чтение / значение по умолчанию open
.
Уведомление. js
const [open, setOpen] = useState(false);
const handleClick = (event) => {
setAnchorEl(event.currentTarget);
if (!open) {
setOpen(true) // set open to true
}
};
useEffect(() => {
socket.on("notification", function (data) {
console.log('SOCKET', open) // idk why this is reading false
})
}, [])
EDIT
Вот мой use-case
handleClick
fun c - это то место, где пользователь загружает свое уведомление, которое установит open
в значение true, а при запуске socket
он проверит, истинно ли open
, и отправит новое уведомление в существующий массив уведомлений
const handleClick = (event) => {
setAnchorEl(event.currentTarget);
if (!open) {
setOpen(true) // set open to true
dispatch(getNotifications(params)) // load user notifications
}
};
useEffect(() => {
socket.on("notification", function (data) {
console.log('SOCKET', open) // returning false always
notificationData(data.data)
})
}, [])
const notificationData = ({ sender, message, notif }) => {
setNotifCounter(notifCounter => notifCounter + 1)
setNotification(_.startCase(sender) + ' - ' + message, 'info')
if (open) { // check if user already loaded his/her own notification
dispatch(getNewNotif(notif)) // push to array to avoid duplicate
}
}