ios 由于某种原因,我的原生应用程序中的onPress按钮无法正常工作

brjng4g3  于 12个月前  发布在  iOS
关注(0)|答案(1)|浏览(112)
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没有触发函数来更改状态

62o28rlo

62o28rlo1#

看起来你的ScreenHeaderBtn组件包含另一个“可点击”的元素,也就是说,如果没有源代码,很难分辨,它优先于TouchableOpacity。这意味着onPress被发送到子组件,而不是TouchableOpacity
下面是一个不可按下的子对象的示例,其中发生了预期的事件:

<TouchableOpacity onPress={onPress}>  
  <Text>Hi</Text>
</TouchableOpacity>

字符串
但是,如果使用Button,它将不再工作:

<TouchableOpacity onPress={onPress}>
  <Button title="Hi" />
</TouchableOpacity>

相关问题