I have a React component that needs to setup a receiver when mounted, and that receiver needs to read a state value, and then set it to a new value. However this is causing the component to mount again (loosing the state values), also interestingly it seems unmount is not fired when this happens.
If i just read the state value it is fine, or if I just write to the state value it is fine, but it seems I can't read it and then write to it in the useEffect? What is the proper way to go about this?
const [currentData, setCurrentData] = useState(null);
// On Mount
useEffect(() => {
console.log("mount dataLoaded")
window.api.receive("dataLoaded", (data) => {
console.log(currentData)
setCurrentData({})
})
}, [currentData]);
// on Unmount
useEffect( () => () => {
console.log("unmount dataLoaded")
window.api.removeListeners("dataLoaded")
}, [] );