I have a react native component that I want to check if a specific value is "true" in scope state, and then do some things but it always read old state in callback function.
const [scope, seScope] = useState({
isInScope: false,
loading: true,
isGranted: false,
})
const stateHandler = useCallback((state) => {
if (state === 'active') {
// it's always false not matter
if (!scope.isGranted) {
makeLocationPermission()
}
}
}, [scope])
console.log(scope.isGranted)
// in here isGranted is true
useEffect(() => {
makeIsGrantedTrue()
AppState.addEventListener('change', stateHandler)
return () => {
AppState.removeEventListener('change', stateHandler)
}
}, [])
ok I solve this just by putting "scope" on use effect dependency. I don't know why ?! it must be on useCallback dependency.
useEffect(() => {
makeIsGrantedTrue()
AppState.addEventListener('change', stateHandler)
return () => {
AppState.removeEventListener('change', stateHandler)
}
}, [scope])
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.