【发布时间】: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