expo / router

[ARCHIVE]: Expo Router has moved to expo/expo -- The File-based router for universal React Native apps

Home Page:https://docs.expo.dev/routing/introduction/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

usePathname usage in layouts breaks production builds at runtime

matthewhausman opened this issue · comments

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

yarn

Summary

Expo router version ^1.4.3

Minimal reproducible example

easily reproducible. run app with expo start --no-dev

works just fine in development. an error here would be great if this is not intended usage because it's very difficult to find the root cause

actually, it is all and any usage of usePathname

usePathname isn't breaking when used in a client component (non navigation related)

Please provide a full Minimal reproducible example and test on Expo Router v2

i can confirm that, i'm using usePathname in a layout to save an history of the visited page in the app and the backhandler to goback on hardwarebackpress.
in production it throws me this error :
com.facebook.react.common.JavascriptException: TypeError: Cannot read property 'pathname' of undefined, js engine: hermes, stack:
since i dont use .pathname anywhere in my code it's clear that inside usePathname doesnt check if the property exists

just saw your code:
/** @returns global selected pathname without query parameters. */ export function usePathname(): string { return useStoreRouteInfo().pathname; }

the problem its probably that in first moment useStoreRouteInfo() cant return anything so using usePathname in a place where this can happen throws the exception

Please provide a reproducible example of the issue so we can investigate further.