I have a grid of buttons with options that I want to be able to select. At first, all values are set at false
. But there's a problem, every time I press a button for the first time, the value is still false
, the second time it's false
again (which is ok) and the third time is where the button is now set to true
. I want to be able to set the value at true
on the first ever time the button is pressed.
const {
// This value is 'false' at first
isArtAndCulture
} = stateController;
const [isAaC, setIsArtAndCulture] = useState(false);
...
<Block style={{ flex: 0.85, marginRight: 5 }}>
<TouchableOpacity
style={{
height: 44,
borderRadius: 5,
alignItems: 'center',
justifyContent: 'center',
backgroundColor: isAaC ? theme.colors.EventSection : theme.colors.Grey
}}
onPress={() => {
setIsArtAndCulture(!isAaC);
// Supposedly the value must be 'true' now, but when I print the 'isArtAndCulture' value,
// it stays 'false' until the third button press.
saveInState({ isArtAndCulture: isAaC });
console.log(isArtAndCulture);
}}
>
<Text
profile
text="Arte y Cultura"
style={{
fontFamily: theme.fonts.Bold,
color: isAaC ? theme.colors.White : theme.colors.Black
}}
/>
</TouchableOpacity>
</Block>
saveInState = state => {
this.setState(state);
}