You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Provide the user with a safe-to-use useSelectedLayoutSegments hook in a client component.
Which should return the URL segments
Non-Goals
No response
Background
The issue is as follows:
const segments = useSelectedLayoutSegments()
the snippet above provides you with an array that includes all the parts in your URL. reference
The problem from what I can deduce is that on each re-render of the component. The hook provides you with a different array (same values, however different references). Which in turn forces a re-render if you are using it as a dependency in useMemo or useEffect.
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Goals
Non-Goals
No response
Background
The issue is as follows:
const segments = useSelectedLayoutSegments()
the snippet above provides you with an array that includes all the parts in your URL. reference
The problem from what I can deduce is that on each re-render of the component. The hook provides you with a different array (same values, however different references). Which in turn forces a re-render if you are using it as a dependency in useMemo or useEffect.
A simple snippet you can reproduce it is.
After running the snippet above in a client component. The console should be flooded.
The only alternative I have found so far is to not use the hook and create it myself.
Here is an example snippet:
Proposal
Make the useSelectedLayoutSegments hook memoized so we can use its tools without the drawbacks.
Beta Was this translation helpful? Give feedback.
All reactions