Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: deprecate <Screen /> redirect prop #677

Merged
merged 2 commits into from Jul 19, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 2 additions & 1 deletion packages/expo-router/src/hooks.ts
Expand Up @@ -11,6 +11,7 @@ import {
useStoreRouteInfo,
} from "./global-state/router-store";
import { Router } from "./types";
import { useDeprecated } from "./useDeprecated";

type SearchParams = Record<string, string | string[]>;

Expand All @@ -28,7 +29,7 @@ export function useRootNavigation() {

// Wraps useLinkTo to provide an API which is similar to the Link component.
export function useLink() {
console.warn("`useLink()` is deprecated in favor of `useRouter()`");
useDeprecated("`useLink()` is deprecated in favor of `useRouter()`");
return useRouter();
}

Expand Down
35 changes: 35 additions & 0 deletions packages/expo-router/src/useDeprecated.ts
@@ -0,0 +1,35 @@
import { useLayoutEffect } from "react";
import { Platform } from "react-native";

// Node environment may render in multiple processes causing the warning to log mutiple times
// Hence we skip the warning in these environments.
const canWarn = Platform.select({
native: process.env.NODE_ENV !== "production",
default:
process.env.NODE_ENV !== "production" && typeof window !== "undefined",
});

const warned = new Set<string>();

export function useWarnOnce(
message: string,
guard: unknown = true,
key = message
) {
// useLayoutEffect typically doesn't run in node environments.
// Combined with skipWarn, this should prevent unwanted warnings
useLayoutEffect(() => {
if (guard && canWarn && !warned.has(key)) {
warned.add(key);
console.warn(message);
}
}, [guard]);
}

export function useDeprecated(
message: string,
guard: unknown = true,
key = message
) {
return useWarnOnce(key, guard, `Expo Router: ${message}`);
}
6 changes: 6 additions & 0 deletions packages/expo-router/src/views/Screen.tsx
@@ -1,5 +1,6 @@
import React from "react";

import { useDeprecated } from "../useDeprecated";
import { useNavigation } from "../useNavigation";

export type ScreenProps<
Expand Down Expand Up @@ -36,6 +37,11 @@ export function Screen<TOptions extends object = object>({
navigation.setOptions(options ?? {});
}, [navigation, options]);

useDeprecated(
marklawlor marked this conversation as resolved.
Show resolved Hide resolved
"The `redirect` prop on <Screen /> is deprecated and will be removed. Please use `router.redirect` instead",
redirect
);

if (process.env.NODE_ENV !== "production") {
// eslint-disable-next-line react-hooks/rules-of-hooks
React.useEffect(() => {
Expand Down
11 changes: 5 additions & 6 deletions packages/expo-router/src/views/Splash.tsx
@@ -1,7 +1,8 @@
import * as SplashModule from "expo-splash-screen";
import { nanoid } from "nanoid/non-secure";
import * as React from "react";
import { Platform } from "react-native";

import { useDeprecated } from "../useDeprecated";

const globalStack: string[] = [];

Expand All @@ -25,11 +26,9 @@ const globalStack: string[] = [];
*/
export function SplashScreen() {
useGlobalSplash();
React.useEffect(() => {
console.warn(
"The <SplashScreen /> component is deprecated. Use `SplashScreen.preventAutoHideAsync()` and `SplashScreen.hideAsync` from `expo-router` instead."
);
}, []);
useDeprecated(
"The <SplashScreen /> component is deprecated. Use `SplashScreen.preventAutoHideAsync()` and `SplashScreen.hideAsync` from `expo-router` instead."
);
return null;
}

Expand Down