I have added Text inside touchableopacity now when user clicks on touchableopacity I want to change color of that button from default gray color to blue color see screenshot below. Now initially checked={this.state.newProjects}
newProjects is not present inside state variable so onclick it should create new variable so that checked
prop becomes true and then color changes. So I need to toggle button color onpress each time. Below code is not working. I am trying to create samething -> https://codesandbox.io/s/k3lzwo27z5 but in react native.
Code:
constructor(props) {
super(props);
this.state = {
};
}
handleClick = e => {
this.setState({
[e.target.name]: !this.state[e.target.name]
});
};
<TouchableOpacity onPress={this.handleClick('newProjects')}>
<Filterbutton name="New Projects" checked={this.state.newProjects}/>
</TouchableOpacity>
Filterbuttonjs:
const Filterbutton = ({name, checked}) => (
<View style={checked ? styles.buttonTab : styles.defaultButtonTab}>
<Text style={styles.buttonContent}>{name}</Text>
</View>
)
export default Filterbutton;
const styles = StyleSheet.create({
defaultButtonTab: {
justifyContent: 'center',
alignItems: 'center',
maxWidth: 150,
height: 30,
padding: 10,
borderRadius: 13,
borderStyle: "solid",
borderWidth: 1.3,
borderColor: "rgba(131, 143, 158, 0.7)",
marginRight: 10,
marginTop: 10
},
buttonTab: {
justifyContent: 'center',
alignItems: 'center',
maxWidth: 150,
height: 30,
padding: 10,
borderRadius: 13,
borderStyle: "solid",
borderWidth: 1.3,
borderColor: "#1994fc",
marginRight: 10,
marginTop: 10
},
Screenshot: