expo: useURL always returns null as a first value even if the app is launched with a deep linking URL
Minimal reproducible example
https://snack.expo.dev/@andrew-tevent/useurl-first-value
Summary
Note: The above snack could just be used to capture the first URL, but I don’t know how/if you can pass a URL to a snack.
It seems there is an issue with Expo Linking - specifically the useURL hook:
https://github.com/expo/expo/blob/main/packages/expo-linking/src/Linking.ts#L358
The react native docs state of getInitialURL that “If the app launch was triggered by an app link, it will give the link url, otherwise it will give null.”
But the useURL hook always returns null on the first execution, which doesn’t seem right as it means you can’t make a correct decision until there has been at least one update to the url value returned.
The issue being that getInitialURL is async.
It might be preferable for url to be string | null | undefined - with undefined for the initial value until the first getInitialURL result has been assigned?
Environment
expo-env-info 1.0.5 environment info: System: OS: Windows 10 10.0.22621 Binaries: Node: 18.13.0 - C:\Program Files\nodejs\node.EXE Yarn: 1.22.19 - ~\AppData\Roaming\npm\yarn.CMD npm: 8.19.3 - C:\Program Files\nodejs\npm.CMD IDEs: Android Studio: AI-213.7172.25.2113.9123335 npmPackages: expo: ^48.0.19 => 48.0.19 react: 18.2.0 => 18.2.0 react-native: 0.71.8 => 0.71.8 Expo Workflow: managed
About this issue
- Original URL
- State: open
- Created a year ago
- Reactions: 2
- Comments: 18 (1 by maintainers)
My workaround is, for now:
This introduces an
undefinedvalue for the URL until it is initially set - which will take place shortly after the call togetInitialURL().Facing the same issue. For some reason I just ended up using the React Context API which seemed to mitigate the issue most of the time
Using SDK 50
apparently this is happen for me when the app is already in the foreground/background but the screen where i call
useURLhasn’t been mounted, and will only be mounted via deeplinking.so i ended up solving this by creating a context
URLProvider.tsxEvery where or any screen in the app
now this is working as expected,
Linking.getInitialUrl()works properly andLinking.addEventListener("url")gets called properly as wellsolution here I’m using for deeplink magic links, it might not suit all use cases but the above did not work for me (for both when the app was in a closed state and in the foreground).
Any update on this?
I tried the temporary workaround and it still returns null. For us,
getInitialURLreturns null when we open the app via a scheme deeplink (myapp://some/path)Have not tried yet with a https deeplink as we can’t set it at the moment, but will update once I’m able to spend some time doing it in the next few days.