I am trying to change boolean state by pressing one of two buttons (from props). The first button triggers handlePermission1
which changes the boolean state to true
and the second button triggers handlePermission2
which changes the boolean to false
. This is main code of the function:
//Variable from Step5
const [permission, setPermission] = useState(true)
function handlePermission1() {
setPermission(true);
setStep(step+1);
console.log(permission);
};
function handlePermission2() {
setPermission(false);
setStep(step+1);
console.log(permission);
};
//Some code...
//...
} if (step===5){
ecraStep=<DefinePermission
onPress1={handlePermission1}
onPress2={handlePermission2}/>
}
//...
However, whenever I try to change state, I need to press one of the buttons TWO TIMES instead of one.
For example, since my initial state is true
, I want to change it to false
so I press the button which triggers handlePermission2
. The console.log exports this:
//The first value
true
//The second value
false
And if I change the initial state to false
and do the same with the other button, the console.log
exports:
//The first value
false
//The second value
true
I would really like to know why this is happening and how should I change the code in order to change the boolean state by pressing a button only once.