Answer the question
In order to leave comments, you need to log in
Is the state already reset when unmounting a component in React Native?
I am writing a small mobile application (I have never done this before, so I apologize for the stupidity), which should track the user's coordinates while working. Accordingly, I subscribe to watchPositionAsync and want to unsubscribe when exiting the screen. The code is something like this (almost everywhere there are console.log stubs to understand what is happening):
function SurveyScreen({ route, navigation }) {
...
const [passengers, setPassengers] = useState(0)
const [location, setLocation] = useState(null)
...
const locCallback = (locationObject) => {
console.log(locationObject.coords)
}
useEffect(() => {
if (locSub == null) {
(async () => {
setLocSub(await Location.watchPositionAsync({ accuracy: Location.Accuracy.Highest, timeInterval: 5000 }, locCallback))
})()
}
return () => {
console.log(`!! ${locSub}`)
if (locSub !== null) {
console.log(`locSub is: ${locSub}`)
console.log(`locSub.remove is: ${locSub.remove}`)
locSub.remove()
}
}
}, [])
useEffect(() => {
...
console.log(locSub)
...
}
}, [passengers])
...
Object {
"remove": [Function remove],
}
Object {
"remove": [Function remove],
}
Object {
"remove": [Function remove],
}
Object {
"remove": [Function remove],
}
!! null
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question