I’m trying to make an app where the background color changes every time you tap the screen. I have the tap event working, but the I don’t know how to change the background color.
Here is my code right now:
import React, {Component} from 'react';
import {
AppRegistry,
StyleSheet,
Text,
View,
TouchableHighlight
} from 'react-native';
let randomHex = () => {
let letters = '0123456789ABCDEF';
let color = '#';
for (let i = 0; i < 6; i++ ) {
color += letters[Math.floor(Math.random() * 16)];
}
return color;
}
export default class randomBackground extends Component {
constructor(props) {
super(props)
this.onClick = this.onClick.bind(this)
}
onClick() {
console.log('clicked ')
}
render() {
return (
<TouchableHighlight onPress={ this.onClick } style={styles.container}>
<View>
<Text style={styles.instructions}>
Tap to change the background
</Text>
</View>
</TouchableHighlight>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
backgroundColor: randomHex()
},
instructions: {
color: "white"
}
});
AppRegistry.registerComponent('randomBackground', () => randomBackground);
I’d like to have the background regenerate every time the screen is tapped.
To change background color on click in React: Set the onClick prop on the element. When the element is clicked, set the active state. Use a ternary operator to conditionally set the background color based on the state variable.
In React Native we can use backgroundColor property of stylesheet to change the screen color to white, black, yellow etc. React Native beginners makes mistake by using background property instead of backgroundColor . This works in React and HTML but not in React native. The hex code for white color is #FFFFFF or #FFF.
You can change the background color by using this.setState
Set the initial background color in your constructor
this.state = {
backgroundColor: randomHex()
}
in your render
function change your style prop to:
[styles.container, {backgroundColor: this.state.backgroundColor}]
and onClick
add
this.setState({backgroundColor: randomHex()});
Heres the full code
import React, { Component } from "react";
import {
AppRegistry,
StyleSheet,
Text,
View,
TouchableHighlight,
} from "react-native";
let randomHex = () => {
let letters = "0123456789ABCDEF";
let color = "#";
for (let i = 0; i < 6; i++) {
color += letters[Math.floor(Math.random() * 16)];
}
return color;
};
export default class randomBackground extends Component {
constructor(props) {
super(props);
this.onClick = this.onClick.bind(this);
this.state = {
backgroundColor: randomHex(),
};
}
onClick() {
console.log("clicked ");
this.setState({ backgroundColor: randomHex() });
}
render() {
return (
<TouchableHighlight
onPress={this.onClick}
style={[
styles.container,
{ backgroundColor: this.state.backgroundColor },
]}
>
<View>
<Text style={styles.instructions}>Tap to change the background</Text>
</View>
</TouchableHighlight>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: "center",
alignItems: "center",
backgroundColor: randomHex(),
},
instructions: {
color: "white",
},
});
AppRegistry.registerComponent("randomBackground", () => randomBackground);
import {
AppRegistry,
StyleSheet,
Text,
View,
TouchableHighlight
} from 'react-native';
export default class randomBackground extends Component {
state={
currentColor:"#FFFFF"
}
onClick() {
let letters = '0123456789ABCDEF';
let color = '#';
for (let i = 0; i < 6; i++ ) {
color += letters[Math.floor(Math.random() * 16)];
}
this.setState({currentColor:color})
}
render() {
return (
<TouchableHighlight onPress={ this.onClick.bind(this) } {[styles.container, {backgroundColor: this.state.currentColor}]}>
<View>
<Text style={styles.instructions}>
Tap to change the background
</Text>
</View>
</TouchableHighlight>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
alignItems: 'center'
},
instructions: {
color: "white"
}
});
AppRegistry.registerComponent('randomBackground', () => randomBackground);
When you simply want to change series of button style change. (example Tab bar buttons, one button selected others not ) simply use conditional styles
style={[this.state.yourstate ? styles.selectedButtonStyle : styles.normalButtonStyle]}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With