1

the problem is quite simple I think, but I can't seem to figure it out. I am using the react-native-router-flux library and the NativeBase library for the buttons.

This is my code:

<Button transparent onPress={Actions.MainOne } style={{ width: 50, height: 50 }} >
       <Text>Option1</Text>
   </Button>

  <Button transparent onPress={Actions.MainTwo} style={{ width: 50, height: 50 }} >
           <Text>Option2</Text>
    </Button>

I want to change the background color of the button whenever i press it and it's active. And if I click another button, then the button I just pressed gets the background and the first button goes back to normal transparent background. I am not really sure how i can add two actions to the button. If anyone can assist I would appreciate it. I don't need to necessarily use the button library, so any ideas about this are welcome !

Thank you !

TheNewbie
  • 323
  • 2
  • 9
  • 22

1 Answers1

0

I use the flux-router to navigate through scenes. This is how I would change the background color of a button when pressed:

    constructor() {
      super();
      state = {
        color1: 'transparent',
        color2: 'transparent',
      }
    }

    setActive(button) {
      if (button === 1) {
        if (this.state.color1 === 'transparent') {
          this.setState({ 
            color1: 'red',
            color2: 'transparent' 
          })
        }
      } else {
        if (this.state.color2 === 'transparent') {
          this.setState({ 
            color2: 'red', 
            color1: 'transparent' 
          })
        }
      }
    }

    { . . . }

    <Button
        onPress={this.setActive.bind(this, 1)}
        style={{ width: 50, height: 50, backgroundColor: this.state.color1 }} 
    >
        <Text>Option1</Text>
    </Button>

    <Button 
        this.setActive.bind(this, 2)
        style={{ width: 50, height: 50, backgroundColor: this.state.color2 }} 
    >
       <Text>Option2</Text>
    </Button>
wvicioso
  • 538
  • 1
  • 3
  • 13