简体   繁体   English

React-native:如何在单击切换开关后更改查看背景颜色

[英]React-native: How to chage View background color once click on a toggle switch

I am trying to change the Background Color of the entire View by a toggle switch.我正在尝试通过切换开关更改整个视图的背景颜色。 But the color is not changing.但是颜色没有变化。 Please help and Big Thanks!请帮助和非常感谢!

Here is my code.这是我的代码。

import { StyleSheet, View, Switch} from 'react-native';
import React, {useState} from 'react';

export default function App() {
const [isEnabled, setIsEnabled] = useState(false);
const toggleSwitch = () => setIsEnabled(previousState => !previousState);
const [color, setColor] = React.useState('yellow');

  return (
    <View style={[styles.container,
                 {backgroundColor:color}]}
          onValueChange = {color => setColor(color)}>

      <Switch onValueChange={toggleSwitch}
              value={isEnabled} onClick={() => setColor('grey')}>
      </Switch>

    </View>
  );
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    flexDirection:'column',
    alignItems:'center',
    justifyContent: 'center'
  }
});

You need to add conditional styling on the view where you want the color to be changed.您需要在要更改颜色的视图上添加条件样式。 Keep it simple and not confuse it.保持简单,不要混淆它。 It would be something like this.会是这样的。

    const [isYellow, setIsYellow] = useState(false)

  return (
      <View style={isYellow ? {backgroundColor: 'yellow'} : {backgroundColor: 'grey'} }>
        <Switch onValueChange={() => setIsYellow(prevYellow => !prevYellow)}
              value={isEnabled}>
        </Switch>
    </View>

You need to define what happens when switch is enabled and what happens when switch is disabled.您需要定义启用开关时会发生什么以及禁用开关时会发生什么。 If enabled (conditional styling) background will be yellow, if not it'll be grey.如果启用(条件样式)背景将为黄色,否则为灰色。

import { StyleSheet, View, Switch} from 'react-native';
import React, {useState} from 'react';

export default function App() {
const [isEnabled, setIsEnabled] = useState(false);
const toggleSwitch = () => setIsEnabled(previousState => !previousState);
const [color, setColor] = useState(true);

  return (    
    <View style={[styles.container, color  ? styles.bgyellow : styles.bggrey]}>
      <Switch onValueChange={toggleSwitch}
              value={isEnabled} onClick={() => setColor(!color)}>
      </Switch>
    </View>
  );
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    flexDirection:'column',
    alignItems:'center',
    justifyContent: 'center'
  },
  bgyellow:{
    background:'yellow'
  },
  bggrey:{
    background:'grey'
  }
});

If you just want to change the Bg-color to grey.如果您只想将背景颜色更改为灰色。

 const [color, setColor] = useState('yellow'); const [isEnabled, setIsEnabled] = useState(false); return( <View style={[styles.container, {backgroundColor: color}]}> <Switch onValueChange={() => { setIsEnabled(;isEnabled); setColor('grey'); }} value={isEnabled}></Switch> </View> )
 <script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script>


If you want to change the Bg-color to "grey" when Switch is disabled and "yellow" when the Switch is enabled如果您想在 Switch 禁用时将 Bg-color 更改为“灰色”,在启用 Switch 时将其更改为“黄色”

 const [color, setColor] = useState('grey'); const [isEnabled, setIsEnabled] = useState(false); const toggleSwitch = () => { setIsEnabled(;isEnabled)? setColor(:isEnabled === true; 'yellow'; 'grey'). }, return( <View style={[styles:container; {backgroundColor: color}]}> <Switch onValueChange={toggleSwitch} value={isEnabled}></Switch> </View> );
 <script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM