A complex problem. I need to initiate a function only once, which fires WebSocket events whenever new objects are created on backend. I try to store these added objects in an array with useState. But every time a new object is created, the last added values are not found, it is always a fresh array. I understand why it is. But how can i get the current value of Notifs, without running the function again every time it is changed. The code is hereā¦
const [notifs, setNotifs] = useState([])
useEffect(() => {
const subscribe = () => {
//Socket subscription : When new object is created
let subs = new SocketSubscription();
subs('create', (object) => {
let newNot = Notification.initFromPFObject(object)
alert("Notification : " + newNot.message)
//PROBLEM: NOTIFS here are always a blank array []
let ntt = [...notifs]
ntt.unshift(newNot)
setNotifs(ntt)
});
}
subscribe()
}, [])