ghvbty
ghvbty

Reputation: 31

I have to create button that will change the style of a text object in React Native App

I have a button, that change textValue of a text object, but I also have to change object's style

this.state = { 
    login_title: 'Initial Value',
    isButtonPressed: true,
  };
      

  this.onPressButton= this.onPressButton.bind(this);
}
 onPressButton() {
    this.setState({
        isButtonPressed : !this.state.isButtonPressed,
        
        login_title : (this.state.isButtonPressed) ? 'Second Value' : 'Intial Value',
    })
}

...

<Button onPress= {this.onPressButton}>
            <ButtonText>Change Value</ButtonText>
        <Text>{this.state.textValue}</Text>

Upvotes: 2

Views: 44

Answers (1)

Dmitri Borohhov
Dmitri Borohhov

Reputation: 1613

In the <Text> widget you can define the style conditionally:

<Text style={this.state.isButtonPressed ? styles.style1 : styles.style2}>
  {this.state.textValue}
</Text>

Of course, this means your style1 and style2 need to be defined.

Upvotes: 1

Related Questions