How to dynamically change button background color in React Native

In this React Native source code example, the source code below illustrate how to dynamically change button background color in React Native.

You can copy and adopt this source code example to your React Native project without reinventing the wheels.

import React, {Component} from 'react';
import {Platform, StyleSheet, Text, View, Button,TouchableOpacity} from 'react-native';

export default class App extends Component {
  state={
    colors:['green', 'blue', 'yellow', 'red'],
    buttonColor:'blue'
  };

  changeColor(){
    const colorArray= this.state.colors;
    var currentColor = colorArray[Math.floor(Math.random() * colorArray.length)];
    this.setState({buttonColor:currentColor});
  }
  render() {
    return (
      <View style={styles.container}>
          <TouchableOpacity
              style={{backgroundColor:this.state.buttonColor, padding: 15}}
              onPress={()=>this.changeColor()}
                >
            <Text style={styles.text}>Change Color!</Text>
          </TouchableOpacity>
      </View>
    );
  }
}

const styles = StyleSheet.create({
  text:{
    color:'white'
    },
  container: {
    flex: 1,
    justifyContent: 'center',
    alignItems: 'center',
    backgroundColor: '#fff',
  },
});

If you have any questions or suggestions kindly use the comment box or you can contact us directly through our contact page below.

 

Add a Comment