Adding debug labels to hooks
Summary: Create a hook to determine the window user's window screen size and update the view based on it. Use the
useDebugValue
hook so that when a component uses the hook three different times, you'll be able to differentiate them on the The React DevTools browser extension.
- Show the current size of the window (
height
andwidth
) - Checkout the RobertBroersma/bigheads package, you'll use this in this exercise!
- Generate three specific avatars, you can name it whatever you want. I named mine,
Mithi
,Diana
, andMikong
. Each specific avatar must have fixed properties but except forhat
,hatColor
,accessory
,clothing
,clothingColor
andgraphic
- Every time the screen size changes, an avatar's
hat
,hatColor
,accessory
,clothing
,clothingColor
andgraphic
should change (randomly). - Only one avatar should be shown for each screen width type, In my case, I show
Mithi
when the screen size isbig
, andDiana
andMikong
formedium
andsmall
respectively. A big screen has a width above1000px
, a small screen has a width below700px
the rest are medium screens. useDebugValue
used to display a label for custom hooks in React DevTools. The custom hook should take in a minimum and maximum width, and return whether the current window size satisfies the condition.
My implementation
Create a useWindowSize
hook
And a useWithinWindowWidth
hook
Here's the function useDebugValue
calls
Here's the component calling useWithinWindowWidth
:
And finally the top level component:
Notes
From React Docs
We don’t recommend adding debug values to every custom Hook. It’s most valuable for custom Hooks that are part of shared libraries.