当单击另一个视图时更改视图的背景颜色。

huangapple go评论53阅读模式
英文:

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视图的背景颜色,并在TouchableHighlightonPress事件中更新该状态,以达到更改背景颜色的目标。

英文:

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 &quot;expo-status-bar&quot;;
import {
  StyleSheet,
  View,
  TouchableHighlight
} from &quot;react-native&quot;;

export default function App() {
  {/*Something like this*/}
  const changeBgColor = () =&gt; {
    styles.wrapper.backgroundColor = &quot;#000000&quot;;
  };
  
  return (
    &lt;View style={styles.wrapper}&gt;
      &lt;TouchableHighlight onPress={changeBgColor}&gt;
        &lt;View style={styles.body}&gt;
          
        &lt;/View&gt;
      &lt;/TouchableHighlight&gt;
    &lt;/View&gt;
  );
}

const styles = StyleSheet.create({
  wrapper: {
    width: 130,
    height: 130,
    padding: &quot;4%&quot;,
    justifyContent: &quot;center&quot;,
    alignItems: &quot;center&quot;,
    backgroundColor: &quot;#ff0000&quot;,
  },
  body: {
    width: &quot;100%&quot;,
    height: &quot;100%&quot;,
    justifyContent: &quot;center&quot;,
    alignItems: &quot;center&quot;,
    backgroundColor: &quot;#ffffff&quot;,
  },
});

<!-- 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 &quot;expo-status-bar&quot;;
import {useState} from &#39;react&#39;;
import {StyleSheet, View, TouchableHighlight} from &#39;react-native&#39;;

export default function App() {
  const [backgroundColor, setBackgroundColor] = useState(
    styles.wrapper.backgroundColor,
  );

  const changeBgColor = () =&gt; {
    setBackgroundColor(&#39;#000000&#39;);
  };

  return (
    &lt;View style={[styles.wrapper, {backgroundColor: backgroundColor}]}&gt;
      &lt;TouchableHighlight onPress={changeBgColor} style={styles.body}&gt;
        &lt;View /&gt;
      &lt;/TouchableHighlight&gt;
    &lt;/View&gt;
  );
}

const styles = StyleSheet.create({
  wrapper: {
    width: 130,
    height: 130,
    padding: &#39;4%&#39;,
    justifyContent: &#39;center&#39;,
    alignItems: &#39;center&#39;,
    backgroundColor: &#39;#ff0000&#39;,
  },
  body: {
    width: &#39;100%&#39;,
    height: &#39;100%&#39;,
    justifyContent: &#39;center&#39;,
    alignItems: &#39;center&#39;,
    backgroundColor: &#39;#ffffff&#39;,
  },
});

huangapple
  • 本文由 发表于 2023年6月22日 04:35:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/76526954.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定