That's the code I wrote to describe my issue,
import React, { useState } from "react";
import { StyleSheet, View, Text, Button } from "react-native";
function HookProblem() {
const [darkMode, setDarkMode] = useState(false);
return (
<View style={{ backgroundColor: darkMode ? "black" : "gray", flex: 1 }}>
<Text style={{ color: darkMode ? "white" : "black" }}>
{darkMode ? "That's Dark Mode" : "That's Light Mode"}
</Text>
<Button title="Dark Mode" onPress={() => setDarkMode(true)} />
<Button title="Light Mode" onPress={() => setDarkMode(false)} />
</View>
);
}
const styles = StyleSheet.create({
con: {
backgroundColor: "yellow",
flex: 1,
},
});
export default HookProblem;
There is no problem until now and everything works very well, but the problem appears when I want to make styleSheet instead of style inline object,
Here
import React, { useState } from "react";
import { StyleSheet, View, Text, Button } from "react-native";
function HookProblem() {
const [darkMode, setDarkMode] = useState(false);
return (
<View style={styles.con}>
<Text style={styles.text}>
{darkMode ? "That's Dark Mode" : "That's Light Mode"}
</Text>
<Button title="Dark Mode" onPress={() => setDarkMode(true)} />
<Button title="Light Mode" onPress={() => setDarkMode(false)} />
</View>
);
}
const styles = StyleSheet.create({
con: {
backgroundColor: darkMode ? "black" : "gray",
flex: 1,
},
text: {
color: darkMode ? "white" : "black",
},
});
export default HookProblem;
ReferenceError: darkMode is not defined, I want to solve this problem if it possible, I want to do something like passing the darkMode boolean outside of the body of the function. Is that possible?