Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React native add active class when push the button

I have a 3 buttons on react native project iOS app. How can I set class active to clicked button and delete this class from others? Like addClass/removeClass that I had used on jquery?

like image 373
Zolotukhin Dmitriy Avatar asked Apr 18 '26 18:04

Zolotukhin Dmitriy


1 Answers

First have your define your style "class".

const styles = StyleSheet.create({
  btnSelected: {
     ...
  },
  notSelected : {
  }
});

Then you can use a state property in your react component.

example :

state = {
  btnSelected: 1
}

<Button 
    style={(this.state.btnSelected== 1)?styles.btnSelected:styles.notSelected}
    onPress={() => this.setState({ btnSelected: 1 })} ... />
<Button 
    style={(this.state.btnSelected== 2)?styles.btnSelected:styles.notSelected} ... 
    onPress={() => this.setState({ btnSelected: 2 })} .../>
<Button 
    style={(this.state.btnSelected== 3)?styles.btnSelected:styles.notSelected} 
    onPress={() => this.setState({ btnSelected: 3 })} ... />
like image 91
L. Carbonne Avatar answered Apr 21 '26 21:04

L. Carbonne