在 React Native 中导航到新屏幕时调用上一个屏幕
Previous screen called when navigating to new one in React Native
我遇到的问题与 here 描述的完全相同。使用选项卡或抽屉在屏幕之间导航时,每个屏幕都会重新加载,导致我的应用出现问题。
我已尝试遵循线程中给出的一些建议,但我无法将其应用到我的代码中。例如:
For me the solution was to nest the navigators as child of the screen component and not inserting the navigator into the component property.
Wrap it in the top level of the file and assign it to a variable, then use that variable inside your component.
我的抽屉由指向 4 个屏幕(主页、历史记录、组、设置)的 4 个快捷方式组成。前三个也可以从主屏幕底部的选项卡中获得。
这是我的代码:
Navigation.js
import React from "react";
import { TouchableOpacity } from "react-native";
import { NavigationContainer} from "@react-navigation/native";
import { createStackNavigator } from "@react-navigation/stack";
import { createBottomTabNavigator } from "@react-navigation/bottom-tabs";
import { createDrawerNavigator } from "@react-navigation/drawer";
import { Entypo } from "@expo/vector-icons";
import Settings from "../screens/Settings";
import Loading from "../screens/Loading";
import AddGroup from "../screens/AddGroup";
import GroupList from "../screens/GroupList";
import Home from "../screens/Home";
import Groups from "../screens/Groups";
import History from "../screens/History";
const HomeStack = createStackNavigator();
const HomeStackScreen = ({ route }) => (
<HomeStack.Navigator}>
<HomeStack.Screen
name={route.name}
component={Home}
/>
</HomeStack.Navigator>
);
const HistoryStack = createStackNavigator();
const HistoryStackScreen = ({ route }) => (
<HistoryStack.Navigator>
<HistoryStack.Screen
name={route.name}
component={History}
/>
</HistoryStack.Navigator>
);
const GroupsStack = createStackNavigator();
const GroupsStackScreen = ({ route }) => (
<GroupsStack.Navigator>
<GroupsStack.Screen
name={route.name}
component={Groups}
/>
</GroupsStack.Navigator>
);
const AppTabs = createBottomTabNavigator();
const bottomTabsScreen = ({ route }) => (
<AppTabs.Navigator>
<AppTabs.Screen
name="Home"
component={HomeStackScreen}
/>
<AppTabs.Screen
name="History"
component={HistoryStackScreen}
/>
<AppTabs.Screen
name="Groups"
component={GroupsStackScreen}
/>
</AppTabs.Navigator>
);
const AppDrawer = createDrawerNavigator();
const AppDrawerScreen = ({ navigation }) => (
<AppDrawer.Navigator>
<AppDrawer.Screen name="Home" component={bottomTabsScreen} />
<AppDrawer.Screen name="History" component={HistoryStackScreen} />
<AppDrawer.Screen name="Groups" component={GroupsStackScreen} />
<AppDrawer.Screen name="Settings" component={Settings} />
</AppDrawer.Navigator>
);
const RootStack = createStackNavigator();
const RootStackScreen = () => {
const [isLoading, setIsLoading] = React.useState(true);
React.useEffect(() => {
setTimeout(() => {
setIsLoading(!isLoading);
}, 500);
}, []);
return (
<RootStack.Navigator mode="modal">
{isLoading ? (
<RootStack.Screen name="Loading" component={Loading} />
) : (
<RootStack.Screen
name="AppDrawerScreen"
component={AppDrawerScreen}
/>
)}
<RootStack.Screen
name="AddGroup"
component={AddGroup}
options={modalOptionStyle}
/>
<RootStack.Screen
name="GroupList"
component={GroupList}
options={({ navigation }) => ({
headerTitle: "Select an existing group",
})}
/>
</RootStack.Navigator>
);
};
export default () => {
return (
<NavigationContainer>
<RootStackScreen />
</NavigationContainer>
);
};
index.js
:
import React from "react";
import Navigation from "./config/Navigation";
import { ConversionContextProvider } from "./util/ConversionContext";
export default () => (
<ConversionContextProvider>
<Navigation />
</ConversionContextProvider>
);
遵循 x00 建议后的最终工作代码:
useEffect(() => {
if (isFocusedGroups) setLoading(true);
const unsubscribe = navigation.addListener("focus", () => {
loadFlatlist("groups")
.then()
.catch((error) => console.error(error))
.finally(() => {
setLoading(false);
});
});
return unsubscribe;
}, [navigation, isFocusedGroups]);
形成你的回购协议:
...
import { useIsFocused } from "@react-navigation/native";
...
const History = ({ navigation }) => {
...
const isFocusedHistory = useIsFocused();
...
useEffect(() => {
...
loadFlatlist("savedRuns")
...
}, [isFocusedHistory]);
...
}
每次用户打开 或 关闭选项卡 isFocusedHistory
更改,因此相应的组件重新呈现并触发效果。
尚未阅读您编写的整个应用程序,也未尝试了解所需的行为是什么,因此无法准确说明您需要如何更改它。但这是你问题的根本原因。
您可能应该放弃在选项卡开关上检索数据的想法。无论如何,这不是一个好主意。
我遇到的问题与 here 描述的完全相同。使用选项卡或抽屉在屏幕之间导航时,每个屏幕都会重新加载,导致我的应用出现问题。
我已尝试遵循线程中给出的一些建议,但我无法将其应用到我的代码中。例如:
For me the solution was to nest the navigators as child of the screen component and not inserting the navigator into the component property.
Wrap it in the top level of the file and assign it to a variable, then use that variable inside your component.
我的抽屉由指向 4 个屏幕(主页、历史记录、组、设置)的 4 个快捷方式组成。前三个也可以从主屏幕底部的选项卡中获得。
这是我的代码:
Navigation.js
import React from "react";
import { TouchableOpacity } from "react-native";
import { NavigationContainer} from "@react-navigation/native";
import { createStackNavigator } from "@react-navigation/stack";
import { createBottomTabNavigator } from "@react-navigation/bottom-tabs";
import { createDrawerNavigator } from "@react-navigation/drawer";
import { Entypo } from "@expo/vector-icons";
import Settings from "../screens/Settings";
import Loading from "../screens/Loading";
import AddGroup from "../screens/AddGroup";
import GroupList from "../screens/GroupList";
import Home from "../screens/Home";
import Groups from "../screens/Groups";
import History from "../screens/History";
const HomeStack = createStackNavigator();
const HomeStackScreen = ({ route }) => (
<HomeStack.Navigator}>
<HomeStack.Screen
name={route.name}
component={Home}
/>
</HomeStack.Navigator>
);
const HistoryStack = createStackNavigator();
const HistoryStackScreen = ({ route }) => (
<HistoryStack.Navigator>
<HistoryStack.Screen
name={route.name}
component={History}
/>
</HistoryStack.Navigator>
);
const GroupsStack = createStackNavigator();
const GroupsStackScreen = ({ route }) => (
<GroupsStack.Navigator>
<GroupsStack.Screen
name={route.name}
component={Groups}
/>
</GroupsStack.Navigator>
);
const AppTabs = createBottomTabNavigator();
const bottomTabsScreen = ({ route }) => (
<AppTabs.Navigator>
<AppTabs.Screen
name="Home"
component={HomeStackScreen}
/>
<AppTabs.Screen
name="History"
component={HistoryStackScreen}
/>
<AppTabs.Screen
name="Groups"
component={GroupsStackScreen}
/>
</AppTabs.Navigator>
);
const AppDrawer = createDrawerNavigator();
const AppDrawerScreen = ({ navigation }) => (
<AppDrawer.Navigator>
<AppDrawer.Screen name="Home" component={bottomTabsScreen} />
<AppDrawer.Screen name="History" component={HistoryStackScreen} />
<AppDrawer.Screen name="Groups" component={GroupsStackScreen} />
<AppDrawer.Screen name="Settings" component={Settings} />
</AppDrawer.Navigator>
);
const RootStack = createStackNavigator();
const RootStackScreen = () => {
const [isLoading, setIsLoading] = React.useState(true);
React.useEffect(() => {
setTimeout(() => {
setIsLoading(!isLoading);
}, 500);
}, []);
return (
<RootStack.Navigator mode="modal">
{isLoading ? (
<RootStack.Screen name="Loading" component={Loading} />
) : (
<RootStack.Screen
name="AppDrawerScreen"
component={AppDrawerScreen}
/>
)}
<RootStack.Screen
name="AddGroup"
component={AddGroup}
options={modalOptionStyle}
/>
<RootStack.Screen
name="GroupList"
component={GroupList}
options={({ navigation }) => ({
headerTitle: "Select an existing group",
})}
/>
</RootStack.Navigator>
);
};
export default () => {
return (
<NavigationContainer>
<RootStackScreen />
</NavigationContainer>
);
};
index.js
:
import React from "react";
import Navigation from "./config/Navigation";
import { ConversionContextProvider } from "./util/ConversionContext";
export default () => (
<ConversionContextProvider>
<Navigation />
</ConversionContextProvider>
);
遵循 x00 建议后的最终工作代码:
useEffect(() => {
if (isFocusedGroups) setLoading(true);
const unsubscribe = navigation.addListener("focus", () => {
loadFlatlist("groups")
.then()
.catch((error) => console.error(error))
.finally(() => {
setLoading(false);
});
});
return unsubscribe;
}, [navigation, isFocusedGroups]);
形成你的回购协议:
...
import { useIsFocused } from "@react-navigation/native";
...
const History = ({ navigation }) => {
...
const isFocusedHistory = useIsFocused();
...
useEffect(() => {
...
loadFlatlist("savedRuns")
...
}, [isFocusedHistory]);
...
}
每次用户打开 或 关闭选项卡 isFocusedHistory
更改,因此相应的组件重新呈现并触发效果。
尚未阅读您编写的整个应用程序,也未尝试了解所需的行为是什么,因此无法准确说明您需要如何更改它。但这是你问题的根本原因。
您可能应该放弃在选项卡开关上检索数据的想法。无论如何,这不是一个好主意。