gpt4 book ai didi

javascript - 如何打印开关的开/关状态?

转载 作者:行者123 更新时间:2023-12-03 07:10:29 24 4
gpt4 key购买 nike

如本例所示:https://reactnative.dev/docs/switch.html我正在尝试打印 Switch 的开/关状态,例如:

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

const App = () => {
const [isEnabled, setIsEnabled] = useState(false);
const toggleSwitch = () => setIsEnabled(previousState => !previousState);

return (
<View style={styles.container}>
<Switch
trackColor={{ false: "#767577", true: "#81b0ff" }}
thumbColor={isEnabled ? "#f5dd4b" : "#f4f3f4"}
ios_backgroundColor="#3e3e3e"
onValueChange={toggleSwitch}
value={isEnabled}
/>
<Text> Switch state is: {isEnabled} </Text>
</View>
);
}

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

export default App;

但是,它不会打印 {isEnabled} 值。

最佳答案

您用于切换状态的代码看起来不错 - 我认为问题更微妙。 bool 值不会在 jsx 中呈现 - 请参阅引用资料:https://reactjs.org/docs/jsx-in-depth.html#booleans-null-and-undefined-are-ignored

你能尝试在渲染之前将 isEnabled 转换为字符串吗?

关于javascript - 如何打印开关的开/关状态?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64142341/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com