#Breakpoint hook issues in client component

2 messages · Page 1 of 1 (latest)

blazing warren
#

I am having an issue using a breakpoint hook to set the aspect ratio of a client component. The hook in question is a custom hook which can be used to get the current breakpoint and set specific values based on that.

This hook worked like a charm with pages but on the app router it seems there is an inconsistency between what is being console.logged and what is rendering on the page.

I have setup a codesandbox to showcase the issue.
Breakpoint hook in question: https://codesandbox.io/p/devbox/keen-fog-zsn369?file=%2Fapp%2Fhooks%2FuseBreakpoint.tsx
Example component using aspect ratios to showcase the issue: https://codesandbox.io/p/devbox/keen-fog-zsn369?file=%2Fapp%2Fcomponents%2Fexample.tsx%3A16%2C15
The sandbox has issues showing the image in the preview window but can see what Im talking about in its own page: https://zsn369-3000.csb.app/

Current behaviour:
Regardless of the screen width, the aspect ratio always renders as 9/16 aspect ratio on initial load. If I resize the screen to a mobile breakpoint and then back to desktop the correct aspect ratios render.

Expected behaviour:
On the initial load, the aspect ratio should be correctly set.

Have tried:
Using a custom state for aspect ratio and a useEffect hook to set the correct aspect ratio. While this works the solution is not ideal as it causes CLS.

Any insights or solutions would be hugely appreciated. fine

Edit* - Added a background color to make it visually a bit more clear

CodeSandbox

CodeSandbox is an online editor tailored for web applications.

late cradleBOT
#

🔎 This post has been indexed in our web forum and will be seen by search engines so other users can find it outside Discord

🕵️ Your user profile is private by default and won't be visible to users outside Discord, if you want to be visible in the web forum you can add the "Public Profile" role in id:customize

✅ You can mark a message as the answer for your post with Right click -> Apps -> Mark Solution
(if you don't see the option, try refreshing Discord with Ctrl + R)