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

WARN The redirect prop on <Screen /> is deprecated and will be removed. Please use router.redirect #834

Closed
gaetan224 opened this issue Aug 10, 2023 · 49 comments · Fixed by expo/expo#23932

Comments

@gaetan224
Copy link

Which package manager are you using? (Yarn is recommended)

yarn

Summary

from the example from expo router docs
when running the for dynamic route

import { Stack, useLocalSearchParams, useRouter } from 'expo-router';

export default function Details() {
  const router = useRouter();
  const params = useLocalSearchParams();

  return (
    <View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}>
      <Stack.Screen
        options={{
          title: params.name,
        }}
      />
      <Text
        onPress={() => {
          router.setParams({ name: 'Updated' });
        }}>
        Update the title
      </Text>
    </View>
  );
}```


i ahev the following error

 WARN  The `redirect` prop on <Screen /> is deprecated and will be removed. Please use `router.redirect` instead
    at Screen (http://192.168.1.6:8081/index.ts.bundle//&platform=ios&dev=true&hot=false&lazy=true:146799:20)
    at RCTView
    at View (http://192.168.1.6:8081/index.ts.bundle//&platform=ios&dev=true&hot=false&lazy=true:40431:43)
    at View



### Minimal reproducible example

import { View, Text } from 'react-native';
import { Stack, useLocalSearchParams, useRouter } from 'expo-router';

export default function Details() {
  const router = useRouter();
  const params = useLocalSearchParams();

  return (
    <View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}>
      <Stack.Screen
        options={{
          title: params.name,
        }}
      />
      <Text
        onPress={() => {
          router.setParams({ name: 'Updated' });
        }}>
        Update the title
      </Text>
    </View>
  );
}
@marklawlor
Copy link
Contributor

Please provide a full reproduction. Creating a new project npx create-expo-app@latest --template tabs@49 and updating app/(tabs)/index to the "Minimal reproducible example" does not reproduce the issue.

I'm going to close this issue, but if you can provide information we can reopen it again.

@sync
Copy link

sync commented Aug 12, 2023

I had a similar issue on my side and what caused it was this code:

<Stack.Screen
        options={{
          title: params.name,
        }}
      />

If you remove it the warning will go away (not a solution I know)

@sync
Copy link

sync commented Aug 12, 2023

I think issue is here perhaps

if you look at the code, if you do this for example:

<Stack.Screen
       options={{
         title: params.name,
       }}
     />

Then deprecated would end up being undefined because redirect would not be set

    useDeprecated(
      "The `redirect` prop on <Screen /> is deprecated and will be removed. Please use `router.redirect` instead",
      redirect
    );

Then looking at useDepecated code here:

export function useDeprecated(
  message: string,
  guard: unknown = true,
  key = message
) {
  return useWarnOnce(key, guard, `Expo Router: ${message}`);
}

the guard would end up being set to true because of the default value guard: unknown = true

Unless I am completely wrong, anyone using stack.screen would end up with a warning, should this issue be reopened ?

PR here: expo/expo#23932

@sync
Copy link

sync commented Aug 12, 2023

This code was introduced with the 2.0.1 release, by default @49 template is using expo-router 2.0.0

@pdandradeb
Copy link

Unless I am completely wrong, anyone using stack.screen would end up with a warning, should this issue be reopened ?

@sync yes, I can confirm that just using <Stack.Screen options={{ title: '' }} /> without redirect triggers this warning.

Reverting to 2.0.0 "fixed" it.

@sync
Copy link

sync commented Aug 16, 2023

yes waiting for someone to review this: expo/expo#23932 :-)

EvanBacon added a commit to expo/expo that referenced this issue Aug 30, 2023
…when using Screen (#23932)

# Why

WARN The redirect prop on <Screen /> is deprecated and will be removed.
Please use router.redirect, fixes
expo/router#834

I think issue is here perhaps
https://github.com/expo/router/blob/f69e31c84a83ceb6d678dfb8aeed708974d301a8/packages/expo-router/src/views/Screen.tsx#L49

if you look at the code, if you  do this for example:

 ```
<Stack.Screen
        options={{
          title: params.name,
        }}
      />
```

Then deprecated prop would end up being `undefined` because `redirect` would not be set

```
    useDeprecated(
"The `redirect` prop on <Screen /> is deprecated and will be removed.
Please use `router.redirect` instead",
      redirect
    );
```

Then looking at `useDepecated` code here: 

```
export function useDeprecated(
  message: string,
  guard: unknown = true,
  key = message
) {
  return useWarnOnce(key, guard, `Expo Router: ${message}`);
}
```
the `guard` would end up being set to `true` because of the default value `guard: unknown = true`


# How

Avoid sending an undefined `redirect` prop to the guard

# Test Plan

Run updated code and make sure warning is gone

# Checklist

<!--
Please check the appropriate items below if they apply to your diff. This is required for changes to Expo modules.
-->

- [x] Documentation is up to date to reflect these changes (eg: https://docs.expo.dev and README.md).
- [x] Conforms with the [Documentation Writing Style Guide](https://github.com/expo/expo/blob/main/guides/Expo%20Documentation%20Writing%20Style%20Guide.md)
- [x] This diff will work correctly for `npx expo prebuild` & EAS Build (eg: updated a module plugin).

---------

Co-authored-by: Evan Bacon <baconbrix@gmail.com>
Co-authored-by: Lucas Caton <lucascaton@gmail.com>
@zackify
Copy link

zackify commented Aug 30, 2023

also confirmed this exists, thanks for the pr

@animaonline
Copy link

When can we expect a fix?

@Dundyne
Copy link

Dundyne commented Sep 12, 2023

Same issue here, pretty annoying

@Pcummings
Copy link

Pcummings commented Sep 12, 2023

Agree - let's get it fixed

In my case it is caused by:

<Stack.Screen
      options={{
          headerShown: false,
 }} />

@sahiljaidka224
Copy link

Can confirm this is still a problem, v 2.0.5

@matthewtesimale
Copy link

Same! Still annoying. It wasn't showing from a project I spun up a few days ago... But now it is showing.

@branaust
Copy link

branaust commented Sep 14, 2023

on expo router 2.0.5 I'm also seeing the issue. I was declaring screen options inline:

return ( <Stack.Screen options={{ headerShown: true, title: "ELLO PUPPET" }} /> ... )

Prior to upgrading, I was able to declare my screen options like this in my component, that is no longer the case

After removing this, and setting the screen options in the declared stack, the warning went away.

@Pcummings
Copy link

on expo router 2.0.5 I'm also seeing the issue. I was declaring screen options inline:

`return (

<Stack.Screen options={{ headerShown: true, title: "ELLO PUPPET" }} />

...

)

`

Prior to upgrading, I was able to declare my screen options like this in my component, that is no longer the case

After removing this, and setting the screen options in the declared stack, the warning went away.

Can you show an example of how you do that?

@branaust
Copy link

branaust commented Sep 15, 2023

on expo router 2.0.5 I'm also seeing the issue. I was declaring screen options inline:
return ( <Stack.Screen options={{ headerShown: true, title: "ELLO PUPPET" }} /> ... )
Prior to upgrading, I was able to declare my screen options like this in my component, that is no longer the case
After removing this, and setting the screen options in the declared stack, the warning went away.

Can you show an example of how you do that?

in (auth)/_layout.tsx

import { Stack } from "expo-router";
import React from "react";

export default function AuthLayout() {
  return (
    <Stack>
      <Stack.Screen
        name="login"
        options={{ headerShown: true, title: "Login" }}
      />
    </Stack>
  );
}

@roiiiu
Copy link

roiiiu commented Sep 17, 2023

on expo router 2.0.5 I'm also seeing the issue. I was declaring screen options inline:
return ( <Stack.Screen options={{ headerShown: true, title: "ELLO PUPPET" }} /> ... )
Prior to upgrading, I was able to declare my screen options like this in my component, that is no longer the case
After removing this, and setting the screen options in the declared stack, the warning went away.

Can you show an example of how you do that?

in (auth)/_layout.tsx

import { Stack } from "expo-router";
import React from "react";

export default function AuthLayout() {
  return (
    <Stack>
      <Stack.Screen
        name="login"
        options={{ headerShown: true, title: "Login" }}
      />
    </Stack>
  );
}

solved!

@joy7426
Copy link

joy7426 commented Sep 21, 2023

Remove this chunk of code from the screen

<Stack.Screen screenOptions={{ headerShown: false }} />

Now you can use this screen configuration in _layout.

@edmbn
Copy link

edmbn commented Sep 22, 2023

Remove this chunk of code from the screen

<Stack.Screen screenOptions={{ headerShown: false }} />

Now you can use this screen configuration in _layout.

This shouldn't be the solution since sometimes there is logic you want to apply from the screen. For example, you may want to modify the headerRight component based on some conditions on the screen (loading data, etc.).

https://docs.expo.dev/router/advanced/stack/#header-buttons

@ycldz
Copy link

ycldz commented Sep 22, 2023

Remove this chunk of code from the screen

<Stack.Screen screenOptions={{ headerShown: false }} />

Now you can use this screen configuration in _layout.

This shouldn't be the solution since sometimes there is logic you want to apply from the screen. For example, you may want to modify the headerRight component based on some conditions on the screen (loading data, etc.).

https://docs.expo.dev/router/advanced/stack/#header-buttons

I agree, it is very useful to use it within the page, this feature should not be removed.

@joy7426
Copy link

joy7426 commented Sep 22, 2023

Remove this chunk of code from the screen

<Stack.Screen screenOptions={{ headerShown: false }} />

Now you can use this screen configuration in _layout.

This shouldn't be the solution since sometimes there is logic you want to apply from the screen. For example, you may want to modify the headerRight component based on some conditions on the screen (loading data, etc.).

https://docs.expo.dev/router/advanced/stack/#header-buttons

I agree but nothing to do! They should have kept both in place.
However, I made my own header and perhaps this is the only solution.

@dhruvr4
Copy link

dhruvr4 commented Sep 22, 2023

When I do
<Stack.Screen
name="login"
options={{ headerShown: true, title: "Login" }}
/>
or <Stack screenOptions = {{ headerShown: true, title: "Login" }}
the warning goes away but the name of the path to the screen for example account/login comes written at the top of the screen

Does anyone know why or how to remove that

@amerllica
Copy link

Is this warning a critical issue or can we ignore it?

charliewhyman added a commit to charliewhyman/travelsplitter that referenced this issue Sep 24, 2023
@danstepanov
Copy link

Is this warning a critical issue or can we ignore it?

you can ignore it until this is removed in future version of expo router, my assumption is that it won't be removed until v3 but I could be wrong.

@danstepanov
Copy link

The solution, although confusing, is to move your declarations to _layout.tsx. @marklawlor correct me if wrong here, I have a reproducible example and can show you the error occurring using npx create-expo-app as well create-expo-stack (more minified version).

return (
  <Stack>
    <Stack.Screen name="one" options={{ title: "One" }} />
    <Stack.Screen name="two"
        options={{ title: "Two", headerLeft: () => <BackButton /> }}
      />
  </Stack>
);

It seems meaningful for Expo Router to continue to support the ability to specify Stack.Screen (as it did in v2.0.0, i believe) on a per screen basis (as this is often required and react-navigation supports it, so expo-router should). I'm curious why the Expo team opted to remove/change this?

@monarcode
Copy link

Having this issue right now and its driving me crazy, hope it gets fixed soon

@NiklasEi
Copy link

If you read the changelog it is marked as resolved in some version around 3.1. This WARNING (not error) is just happening in the current version.

The fix is merged for version 3 in the expo/expo repository. The linked PR is the backport of that fix for version 2, which is the "latest" version on npmjs.

The fact that this is "just" a warning doesn't help here. It causes a pop-up in developer mode and spams my console.

@markedwards
Copy link

Why can this not be backported to a 2.0.x release? This is incredibly annoying.

@rskvazh
Copy link

rskvazh commented Sep 26, 2023

Same here. Very frustrated.

@mcsrk
Copy link

mcsrk commented Sep 26, 2023

At this point lets just switch to react nav

@adekorir
Copy link

frustrating.

@Brian-McBride
Copy link

How else do you change the header components in a dynamic view?

app/(shop)/(product)/[id].js as an example.

I have

  <Stack.Screen
    options={{
      title: data?.name ?? '',
    }}
  />

Producing the same error as everyone else.

I just need to set the title of my view based on the data that I've fetched.

@leanderlee
Copy link

Can this issue be reopened? This seems important to fix.

@zavbala
Copy link

zavbala commented Sep 28, 2023

Re-open it, please 🙃

@leanderlee
Copy link

Can this issue be reopened? This seems important to fix.

pinging @marklawlor, in case.

@leggomuhgreggo
Copy link

leggomuhgreggo commented Sep 29, 2023

Here's a different workaround that has the advantage of maintaining the "page + options" colocation.

import React from 'react';
import { View} from 'react-native';
import { Link, Stack } from 'expo-router';
- import { Stack } from 'expo-router';
+ import { useNavigation } from 'expo-router';

export const HomeScreen = () => {
+  const navigation = useNavigation();
+
+  React.useEffect(() => {
+    navigation.setOptions({
+      headerShown: false,
+    });
+  }, [navigation]);

  return (
    <View>
-      <Stack.Screen options={{ headerShown: false }} />

      <Text>Home</Text>
    </View>
  );
};

If that doesn't work whatever reason, the other workaround is is to move the <Stack.Screen options={...}> to the parent <Stack /> in _layout.tsx (see this comment and this comment)

Hope that helps!

EDIT: Opened PR to update <Stack> docs with example

@zackify
Copy link

zackify commented Oct 4, 2023

If you want to keep doing stuff the old way in v2, you can patch it yourself. Someone asked me above how to do it so here's how:

just install patch-package from npm, or use the built in approach if your package manager supports it.

All you have to do is add != null in the file referenced in the merged pr: expo/expo#23932 to get this to go away

made a short blog post on it if anyone needs more help than that.

@wvq
Copy link

wvq commented Oct 5, 2023

Here's a different workaround that has the advantage of maintaining the "page + options" colocation.

import React from 'react';
import { View} from 'react-native';
import { Link, Stack } from 'expo-router';
- import { Stack } from 'expo-router';
+ import { useNavigation } from 'expo-router';

export const HomeScreen = () => {
+  const navigation = useNavigation();
+
+  React.useEffect(() => {
+    navigation.setOptions({
+      headerShown: false,
+    });
+  }, [navigation]);

  return (
    <View>
-      <Stack.Screen options={{ headerShown: false }} />

      <Text>Home</Text>
    </View>
  );
};

If that doesn't work whatever reason, the other workaround is is to move the <Stack.Screen options={...}> to the parent <Stack /> in _layout.tsx (see this comment and this comment)

Hope that helps!

EDIT: Opened PR to update <Stack> docs with example

With this example, there has a display issue on my cell phone.
During the screen navigation animation, header is show, then hidden.
Use <Screen options={{ headerShown: false }}> the header is default hidden.

@lesthoward
Copy link

lesthoward commented Oct 5, 2023

The solution, although confusing, is to move your declarations to _layout.tsx. @marklawlor correct me if wrong here, I have a reproducible example and can show you the error occurring using npx create-expo-app as well create-expo-stack (more minified version).

return (
  <Stack>
    <Stack.Screen name="one" options={{ title: "One" }} />
    <Stack.Screen name="two"
        options={{ title: "Two", headerLeft: () => <BackButton /> }}
      />
  </Stack>
);

It seems meaningful for Expo Router to continue to support the ability to specify Stack.Screen (as it did in v2.0.0, i believe) on a per screen basis (as this is often required and react-navigation supports it, so expo-router should). I'm curious why the Expo team opted to remove/change this?

@danstepanov You are right. I did the same thing to solve this issue, as same as you. So, yes, this works!

@balloman
Copy link

balloman commented Oct 6, 2023

Why is this issue closed??

@markedwards
Copy link

I'm going to close this issue, but if you can provide information we can reopen it again.

@marklawlor, this is easily reproducible, can you please reopen?

SamSamskies added a commit to wavlake/mobile that referenced this issue Oct 6, 2023
@nabilfreeman
Copy link

nabilfreeman commented Oct 6, 2023

I guess between moving Expo Router upstream into the main Expo repo, and the imminent release of v3 the team are ignoring or deprioritising anything related to v2.

I know we are on the bleeding edge but I think it’s a bit cavalier to leave v2 in an imperfect state like this and expect people to patch or do a major version upgrade.

I think it damages adoption, because v3 is not live yet and new users are installing this every day and possibly turning away because of small issues like this.

🤷‍♂️

SamSamskies added a commit to wavlake/mobile that referenced this issue Oct 6, 2023
blastshielddown pushed a commit to wavlake/mobile that referenced this issue Oct 6, 2023
@mattphillips
Copy link

As a workaround, while we wait for this to be patched in v2 I've found that you can suppress the warning by adding the following to the root of your app:

import { LogBox } from 'react-native';

LogBox.ignoreLogs([
  'The `redirect` prop on <Screen /> is deprecated and will be removed. Please use `router.redirect` instead'
]);

Not the best solution but saves faffing with patch package etc.

@tholst
Copy link

tholst commented Oct 7, 2023

fyi: it seems to be fixed now in expo-router@2.0.9

(see commit 9f8a67b)

@david7378
Copy link

sup

Yes its fixed!! Thanks!!

amandeepmittal added a commit to expo/expo that referenced this issue Oct 22, 2023
Adds section to stack.mdx showing alternative method to configure screen
options from route, via imperative API

# Why


Clarifying feature options following discussion in this issue:
expo/router#834 (comment)

# Checklist

<!--
Please check the appropriate items below if they apply to your diff.
This is required for changes to Expo modules.
-->

- [x] Documentation is up to date to reflect these changes (eg:
https://docs.expo.dev and README.md).
- [x] Conforms with the [Documentation Writing Style
Guide](https://github.com/expo/expo/blob/main/guides/Expo%20Documentation%20Writing%20Style%20Guide.md)
- [x] This diff will work correctly for `npx expo prebuild` & EAS Build
(eg: updated a module plugin).

---------

Co-authored-by: Aman Mittal <amandeepmittal@live.com>
marklawlor pushed a commit to expo/expo that referenced this issue Oct 30, 2023
Adds section to stack.mdx showing alternative method to configure screen
options from route, via imperative API

# Why


Clarifying feature options following discussion in this issue:
expo/router#834 (comment)

# Checklist

<!--
Please check the appropriate items below if they apply to your diff.
This is required for changes to Expo modules.
-->

- [x] Documentation is up to date to reflect these changes (eg:
https://docs.expo.dev and README.md).
- [x] Conforms with the [Documentation Writing Style
Guide](https://github.com/expo/expo/blob/main/guides/Expo%20Documentation%20Writing%20Style%20Guide.md)
- [x] This diff will work correctly for `npx expo prebuild` & EAS Build
(eg: updated a module plugin).

---------

Co-authored-by: Aman Mittal <amandeepmittal@live.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.