import { useState } from "react";
import {
View,
ScrollView,
SafeAreaView,
Text,
TouchableOpacity,
StyleSheet,
Modal,
} from "react-native";
import { Stack, useRouter } from "expo-router";
import { COLORS, icons, images, SIZES } from "../constants";
import {
Nearbyjobs,
Popularjobs,
ScreenHeaderBtn,
Welcome,
} from "../components";
const Home = () => {
const router = useRouter();
const [searchTerm, setSearchTerm] = useState("");
const [openNav, setOpenNav] = useState(false);
const toggleNavigation = () => {
setOpenNav(false);
};
return (
<SafeAreaView style={{ flex: 1, backgroundColor: COLORS.lightWhite }}>
<Stack.Screen
options={{
headerStyle: {
backgroundColor: COLORS.lightWhite,
},
headerShadowVisible: false,
headerLeft: () => (
<TouchableOpacity onPress={toggleNavigation}>
<ScreenHeaderBtn iconUrl={icons.menu} dimension="60%" />
</TouchableOpacity>
),
headerRight: () => (
<ScreenHeaderBtn iconUrl={images.profile} dimension="100%" />
),
headerTitle: "Jobbed",
}}
/>
<ScrollView showsVerticalScrollIndicator={false}>
{openNav ? (
<Text>Nav is working</Text>
) : (
<Text>Not working error !!!!!</Text>
)}
<View style={{ flex: 1, padding: SIZES.medium }}>
<Welcome
searchTerm={searchTerm}
setSearchTerm={setSearchTerm}
handleClick={() => {
if (searchTerm) {
router.push(`/search/${searchTerm}`);
}
}}
/>
<Popularjobs />
<Nearbyjobs />
</View>
</ScrollView>
</SafeAreaView>
);
};
const styles = StyleSheet.create({
modalContainer: {
flex: 1,
justifyContent: "center",
alignItems: "center",
backgroundColor: "rgba(0, 0, 0, 0.5)", // semi-transparent background
},
menu: {
backgroundColor: COLORS.lightWhite,
padding: SIZES.medium,
borderRadius: 10,
},
});
export default Home;
字符串
我想有一个导航栏组件,但即时通讯测试它只是一个普通的视图,现在,onPress不触发使用状态,我不知道为什么。看看我如何测试它。
{openNav ? (
<Text>Nav is working</Text>
) : (
<Text>Not working error !!!!!</Text>
)}
型
通用代码正在工作,因为它显示了状态为false时的选项,现在的问题是onpress没有触发函数来更改状态
1条答案
按热度按时间62o28rlo1#
看起来你的
ScreenHeaderBtn
组件包含另一个“可点击”的元素,也就是说,如果没有源代码,很难分辨,它优先于TouchableOpacity
。这意味着onPress
被发送到子组件,而不是TouchableOpacity
。下面是一个不可按下的子对象的示例,其中发生了预期的事件:
字符串
但是,如果使用
Button
,它将不再工作:型