【问题标题】:How to get information when the switch is flipped React-Native切换开关时如何获取信息 React-Native
【发布时间】:2021-11-22 00:15:12
【问题描述】:

我想获取开关是否被切换的信息。并将此信息导入其他文件。然后我想对 StyleSheet 进行 IF 查询。


const DarkLightSwitch = () => {
  const [lock, setLock] = useState();
  return (
          <Text style={styles.Zeit}>Darkmode</Text>
          <View style={styles.container}>
            <Switch
              trackColor={{ false: "#02131C", true: "#02131C" }}
              thumbColor={isEnabled ? "#FEF5E2" : "#FFF"}
              ios_backgroundColor={isEnabled ? "#02131C" : "#FEF5E2"}
              onValueChange={toggleSwitch}
              value={isEnabled}
              disabled={lock ? "false" : "true"}
            />
          </View>
  );
};

另一个文件:

import { isEnabled } from "../../Einstellungen/Dark-LightSwitch";

const styles = StyleSheet.create({
  container: {
        backgroundColor: isEnabled === "false" ? "#02131C" : "#FEF5E2",
      }
});

【问题讨论】:

  • 我认为您不能只从另一个文件中导入该值并在更改时收到通知。当您想在另一个组件中使用它时,您可以将其作为 props 传递,或者将值传递给您的 Redux State

标签: reactjs react-native stylesheet switching


【解决方案1】:

你不能在 StyleSheet.create 中拥有动态样式

解决方法是这样的

const styles = StyleSheet.create({
  container: {
        // styles that are not background color
      }
});

<View style={[styles.container, { backgroundColor: isEnabled ? "#02131C" : "#FEF5E2" }]}>

注意,这里也不需要 === false

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-27
    • 2021-09-17
    • 2018-09-13
    • 2018-03-23
    • 2021-10-06
    相关资源
    最近更新 更多