tamagui: useMedia hook having wrong initial value in some situations

Current Behavior

  1. Using the useMedia hook in a _layout.tsx file
  2. Having a conditional return above the access of the proxy e.g. media.sm
  3. In some cases (I was not able to reproduce it yet with the starter template) this leads to the hook not updating correctly after refresh
  4. In the provided example code isLoading is initialized with true and after some time changes to false.
  5. When media.sm is accessed before the conditional return it updates correctly e.g. adding console.log(media.sm) directly after the hook (which I guess defeats the purpose of static optimizations)

Expected Behavior

No response

Tamagui Version

1.101.7

Platform (Web, iOS, Android)

Web

Reproduction

const {session, isLoading, signOut} = useSession();
const media = useMedia();

if (isLoading) {
  return <Text>Loading...</Text>; // This early return leads to useMedia not updating
}

console.log('media.sm in _layout.tsx', media.sm);
return <Slot />

What fixes it:

const {session, isLoading, signOut} = useSession();
const media = useMedia();
console.log('media.sm in _layout.tsx', media.sm);

if (isLoading) {
  return <Text>Loading...</Text>; // This early return leads to useMedia not updating
}

return <Slot />


### System Info

_No response_

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Reactions: 5
  • Comments: 15 (5 by maintainers)

Most upvoted comments

can confirm this works when using disableSSR: true

Yeah but it is causing other issues on other components with hydration. We need a proper way to handle it @natew