React Native 使用expo路由器避免嵌套导航中多次调用useEffect

0g0grzrc  于 2023-03-19  发布在  React
关注(0)|答案(1)|浏览(211)

我目前正在尝试用expo-router进行布局,结构如下:

  • 应用程序
  • _布局(堆栈)
  • _布局(制表符)
  • 指标
  • 剖面
  • 地点
  • _布局(堆栈)
  • [id](演示文稿:“模态”)
  • 评论
  • _布局(堆栈)
  • [编号]

你明白我的意思。
现在下面的问题:
如果我登录,我会被重定向到/home/index。在那个组件中,我运行了一个useEffect钩子来获取一些数据。现在那个钩子被调用了两次,我不知道为什么...
另一方面,如果我路由到/place/1(它是一个模态,与标签导航重叠,并在父堆栈导航器中注册),然后查看/1(另一个堆栈),useEffect的行为与预期的一样,只运行一次。但一旦我关闭home/index前面的模态(place/1),home/index的useEffect就会再次被调用两次...
应用程序布局(_L):

<AuthProvider>
      <SafeAreaView style={{ flex: 1, backgroundColor: "white", marginBottom: -35 }}>
        <Stack screenOptions={{ headerShown: false }}>
          <Stack.Screen name="(auth)" />
          <Stack.Screen name="(modals)/addNewModal" options={{ presentation: "modal" }} />
          <Stack.Screen
            name="(modals)/searchForPlaceModal"
            options={{
              presentation: "transparentModal",
              animation: "fade",
              animationDuration: 2000,
            }}
          />
          <Stack.Screen name="place" options={{ presentation: "modal" }} />
          <Stack.Screen
            name="(modals)/mapViewModal"
            options={{ presentation: "fullScreenModal", animation: "slide_from_bottom" }}
          />
          <Stack.Screen name="home" />
        </Stack>
      </SafeAreaView>
    </AuthProvider>

主页布局(_L):

<Tabs
      screenOptions={{
        headerShown: false,
        tabBarStyle: { height: 90 },
        tabBarShowLabel: false,
      }}
      initialRouteName="index"
    >
      <Tabs.Screen
        name="index"
        options={{
          tabBarAllowFontScaling: true,
          tabBarIcon: ({ color, focused, size }) => (
            <TabBarIcon focused={focused} iconName="search1" labelname="Stöbern" />
          ),
        }}
      />
      <Tabs.Screen
        name="favorites"
        options={{
          tabBarAllowFontScaling: true,
          tabBarIcon: ({ color, focused, size }) => (
            <TabBarIcon focused={focused} iconName="hearto" labelname="Favoriten" />
          ),
        }}
      />
      <Tabs.Screen
        listeners={{
          tabPress: (e) => {
            e.preventDefault();
            router.push("/addNewModal");
          },
        }}
        name="addNewModal"
        options={{
          tabBarAllowFontScaling: true,
          tabBarIcon: ({ color, focused, size }) => (
            <TabBarIcon focused={focused} iconName="pluscircle" customSize={60} labelname="" />
          ),
        }}
      />
      <Tabs.Screen
        name="social"
        options={{
          tabBarAllowFontScaling: true,
          tabBarIcon: ({ color, focused, size }) => (
            <TabBarIcon focused={focused} iconName="team" labelname="Community" />
          ),
        }}
      />
      <Tabs.Screen
        name="profile"
        options={{
          tabBarAllowFontScaling: true,
          tabBarIcon: ({ color, focused, size }) => <TabBarIcon focused={focused} iconName="user" labelname="Profil" />,
        }}
      />
    </Tabs>

这真的让我很困扰。希望有人能解决这个问题。:)
我禁用了React严格模式。
已检查ParentComponent更新。
什么也没发现,只是如果我来回路由到嵌套的导航堆栈,它似乎正在发生......
下面是我的索引文件和useEffect:

import { useFocusEffect, useRouter } from "expo-router";
import { ScrollView, Text, View } from "react-native";
import { useEffect, useState } from "react";
import { Ionicons } from "@expo/vector-icons";
import { TouchableOpacity } from "react-native-gesture-handler";
import QuickSearchBar from "../../src/components/home/quickSearchBar";
import ListingComponent from "../../src/components/home/listingComponent";
import SearchBar from "../../src/components/searchBar/SearchBar";
import React from "react";

export default React.memo(function Index() {
  const router = useRouter();
  const [userId, setUserId] = useState(null);

  function getPermissionAndSetLocation() {
    // let { status, ios } = await Location.requestForegroundPermissionsAsync();
    // if (status !== "granted") {
    // } else {
    //   let location = await Location.getCurrentPositionAsync({
    //     accuracy: Device.osName === "Android" ? Location.Accuracy.Low : Location.Accuracy.Lowest,
    //   });
    // }
  }

  useEffect(() => {
    console.log("index home");
  }, []);

  return (
    <View className="flex-1 bg-white relative">
      <View className=" border-b-gray-100 border-b">
        <SearchBar />
        <QuickSearchBar />
      </View>
      <ScrollView showsVerticalScrollIndicator={false} scrollEnabled>
        <ListingComponent />
      </ScrollView>
      <TouchableOpacity onPress={() => router.push("/mapViewModal")}>
        <View className="bg-darkGrey flex-shrink mx-auto mb-5 px-4 py-2 space-x-2 justify-center items-center flex-row rounded-lg">
          <Ionicons name="map" size={30} color="white" />
          <Text className="text-white">Karte</Text>
        </View>
      </TouchableOpacity>
    </View>
  );
});

[![项目结构][1]][1] [1]:https://i.stack.imgur.com/v3jes.png

gc0ot86w

gc0ot86w1#

发现这是一个expo路由器的问题。正如在GitHub上当前的issue中发现的那样。
将观察他们的进展,并使用曝光路由器时,稳定。

相关问题