I just came across with the weird issue in my application. I have defined state in a functional component, I am able to update the state which i am clearly able to see through useEffect by passing it as dependency. But when i am calling the state inside handleValidatePin
function it is showing the initial value.
const ChangePin=(props:Props)=>{
const [pin, setPIN] = useState("");
console.log("Calling", pin); //printing updated state
function handleValidatePin() {
console.log("PIN:", pin); // printing initial state [Empty String]
setLoading(true);
if (pin !== PIN) {
pinInputRef.current?.shake();
setLoading(false);
} else {
setTimeout(() => {
setPINLocal(pin);
setLoading(false);
setToast({ type: "success", message: "PIN changed successfully!" });
props.handleCloseSheet();
}, 2000);
}
}
useEffect(() => {
console.log("Updated PIN:", pin); //printing updated state
}, [pin]);
return(
<View/>
.....
</View
)
}