I declare and initialize the variable like this
const [idOld, setIdOld] = useState(null)
Then when I call the function testing()
on Button Press
const testing = () => {
setIdOld(535)
alert(idOld)
}
return (
<View>
<Text>Home</Text>
<CustomFloatingLogout bottom={80} onPress={() => testing()} />
<CustomFloatingLogout onPress={() => logout()} />
</View>
)
The alert shows the variable is null.
How to solve this?
CodePudding user response:
One of the option is to utilize useEffect
for it
const [idOld, setIdOld] = useState(null)
useEffect(() => {
alert(idOld)
}, [idOld]);
return (
<View>
<Text>Home</Text>
<CustomFloatingLogout bottom={80} onPress={() => setIdOld(535)} />
<CustomFloatingLogout onPress={() => logout()} />
</View>
)