My button does no change color. Here s my code:
import React from 'react';
import { View, Text, StyleSheet, TouchableOpacity } from 'react-native';
const MyButton = () => {
const [isPressed, setIsPressed] = React.useState(false);
const handlePressIn = () => {
setIsPressed(true);
};
const handlePressOut = () => {
setIsPressed(false);
};
return (
<TouchableOpacity
onPressIn={handlePressIn}
onPressOut={handlePressOut}
style={[styles.button, isPressed && styles.buttonPressed]}
>
<Text style={styles.buttonText}>Press Me</Text>
</TouchableOpacity>
);
};
const styles = StyleSheet.create({
button: {
backgroundColor: 'blue',
padding: 10,
borderRadius: 5,
},
buttonPressed: {
backgroundColor: 'red',
},
buttonText: {
color: 'white',
fontSize: 16,
},
});
export default MyButton;
What am I doing wrong? Button doesno change color. Need help. Thank you!your text
I try to change the color when button pressed. Expect button turn red but stay blue.
New contributor
Anastasios3 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.