esta es mi primera pregunta, así que básicamente tengo un objeto de notificación y he adjuntado una base de fuego onValue listners que activa la función handleNotification cada vez que se agrega un nuevo valor, el problema es que handleNotification fn no está usando el estado de notificación actualizado
const [notification, setNotification] = useState({ id: '', total: '', lastTotal: '', count: '', }); const handleNotification = (channelId, snap, notification) => { console.log('notification before -------', notification); if (notification.id === '') { let obj = { id: channelId, total: snap.size, lastKnownTotal: snap.size, count: 0, }; setNotification({ ...obj }); return console.log('notification after -------', notification); } // the above if statement runs even when notification.id is already populated, the below code should run instead of that let obj2 = { ...notification, }; if (snap.size - obj2.total > 0) { setNotification((prev) => ({ ...prev, count: snap.size - obj2.total })); } }; useEffect(() => { const unsubscribe = onValue( ref(db, 'messages' + '/' + channel.id), (snap) => { if (channel) { handleNotification(channel.id, snap, notification); } else { console.log('////'); } } ); return () => { unsubscribe(); }; }, []); 

Las actualizaciones de estado ocurren de forma asíncrona, es por eso que todavía está viendo el estado anterior. Puede colocar el archivo console.log fuera de la función, que generará el estado actualizado.
const handleNotification = (channelId, snap, notification) => { console.log('notification before -------', notification); if (notification.id === '') { let obj = { id: channelId, total: snap.size, lastKnownTotal: snap.size, count: 0, }; setNotification({ ...obj }); return; } // the above if statement runs even when notification.id is already populated, the below code should run instead of that let obj2 = { ...notification, }; if (snap.size - obj2.total > 0) { setNotification((prev) => ({ ...prev, count: snap.size - obj2.total })); } }; console.log('notification after -------', notification);