`usePathname()` not returning appropriate path when routing back to '/'
Answered
Japanese Chin posted this in #help-forum
Japanese ChinOP
Hi all, I am attempting to use
const [currentPath] = useState(usePathname()) when returning from a page back to the index page, but the value returned is the same as the previous path. For example, if I go to /favourites and then return to / it will return /favourites. Is that due to the way the component is rendering? Appreciate any responses in advance.11 Replies
because you put it in state but never changing it?
Northern Wheatear
This is because u have used it as initial value for the state
Japanese ChinOP
So state isn't reset when re-rendering? It works fine when I switch between pages via the router, just returning back to the index file seems to not change. Does that mean that the index file isn't re-rendering?
I take it I need to use a
useEffect, then?just use
const currentPath = usePathname()Answer
are you putting it in a layout.tsx? layout won't rerender on navigation
Japanese ChinOP
It's in page, but that did work, thank you. What's the use case of
useState if you can use variables, then? Is state used for data that'll change with interaction?usePathname itself returning a state i thinkJapanese ChinOP
Understood, I appreciate your patience! New to all this so I thought I'd ask rather then ponder. Have a great rest of the day ðŸ¤
np please mark solution