英文:
change the background color of view when clicked on another view
问题
我是新手使用react-native,所以请谅解。我正在尝试在单击另一个视图元素时更改视图元素的背景颜色。我有一个名为"wrapper"的视图元素和另一个名为"body"的视图元素。我想要实现的目标是在单击"body"视图时更改"wrapper"视图的backgroundColor属性。我该如何实现这一点?我尝试下面的方法会引发错误"尝试分配给只读属性"。正确的做法是什么?提前感谢。
import { StatusBar } from "expo-status-bar";
import {
StyleSheet,
View,
TouchableHighlight
} from "react-native";
export default function App() {
const changeBgColor = () => {
// 请注意,这里的styles对象不可变,无法直接修改背景颜色。
// 您应该使用组件状态来实现这一目标。
};
return (
<View style={styles.wrapper}>
<TouchableHighlight onPress={changeBgColor}>
<View style={styles.body}>
</View>
</TouchableHighlight>
</View>
);
}
const styles = StyleSheet.create({
wrapper: {
width: 130,
height: 130,
padding: "4%",
justifyContent: "center",
alignItems: "center",
backgroundColor: "#ff0000",
},
body: {
width: "100%",
height: "100%",
justifyContent: "center",
alignItems: "center",
backgroundColor: "#ffffff",
},
});
请注意,上述代码中的changeBgColor
函数尝试直接更改样式,但这是不可行的。您应该使用组件的状态(useState)来管理wrapper
视图的背景颜色,并在TouchableHighlight
的onPress
事件中更新该状态,以达到更改背景颜色的目标。
英文:
I am new to react-native so do bear with me. I am trying to change the background color of a view element when I click on a separate view element. I have a view element named wrapper and another view element named body. What I want to achieve is to change the backgroundColor property of the view "wrapper" when I click on the view "body". How can I achieve this? What I tried below throws error Attempted to assign to readonly property.
What is the proper way to do this? Thanks in advance
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
import { StatusBar } from "expo-status-bar";
import {
StyleSheet,
View,
TouchableHighlight
} from "react-native";
export default function App() {
{/*Something like this*/}
const changeBgColor = () => {
styles.wrapper.backgroundColor = "#000000";
};
return (
<View style={styles.wrapper}>
<TouchableHighlight onPress={changeBgColor}>
<View style={styles.body}>
</View>
</TouchableHighlight>
</View>
);
}
const styles = StyleSheet.create({
wrapper: {
width: 130,
height: 130,
padding: "4%",
justifyContent: "center",
alignItems: "center",
backgroundColor: "#ff0000",
},
body: {
width: "100%",
height: "100%",
justifyContent: "center",
alignItems: "center",
backgroundColor: "#ffffff",
},
});
<!-- end snippet -->
答案1
得分: 1
你可以通过改变状态来改变背景颜色。你必须使用状态来控制背景颜色;当你想要改变它时,可以通过 setState
来改变状态。
在你的示例中,我已经在 viewBGColor
状态中设置了默认的背景颜色,并在点击按钮时改变了背景颜色状态。
import { StatusBar } from "expo-status-bar";
import { useState } from 'react';
import { StyleSheet, View, TouchableHighlight } from 'react-native';
export default function App() {
const [backgroundColor, setBackgroundColor] = useState(
styles.wrapper.backgroundColor,
);
const changeBgColor = () => {
setBackgroundColor('#000000');
};
return (
<View style={[styles.wrapper, { backgroundColor: backgroundColor }]}>
<TouchableHighlight onPress={changeBgColor} style={styles.body}>
<View />
</TouchableHighlight>
</View>
);
}
const styles = StyleSheet.create({
wrapper: {
width: 130,
height: 130,
padding: '4%',
justifyContent: 'center',
alignItems: 'center',
backgroundColor: '#ff0000',
},
body: {
width: '100%',
height: '100%',
justifyContent: 'center',
alignItems: 'center',
backgroundColor: '#ffffff',
},
});
注意:上述代码是用于改变背景颜色的示例代码。
英文:
You can change the background color by changing the state. you have to use state for the background color; when you want to change it, you can change state by setState.
Here in your example, I have set the default background color in viewBGColor state and changed the background color state on the click button.
import { StatusBar } from "expo-status-bar";
import {useState} from 'react';
import {StyleSheet, View, TouchableHighlight} from 'react-native';
export default function App() {
const [backgroundColor, setBackgroundColor] = useState(
styles.wrapper.backgroundColor,
);
const changeBgColor = () => {
setBackgroundColor('#000000');
};
return (
<View style={[styles.wrapper, {backgroundColor: backgroundColor}]}>
<TouchableHighlight onPress={changeBgColor} style={styles.body}>
<View />
</TouchableHighlight>
</View>
);
}
const styles = StyleSheet.create({
wrapper: {
width: 130,
height: 130,
padding: '4%',
justifyContent: 'center',
alignItems: 'center',
backgroundColor: '#ff0000',
},
body: {
width: '100%',
height: '100%',
justifyContent: 'center',
alignItems: 'center',
backgroundColor: '#ffffff',
},
});
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论