router icon indicating copy to clipboard operation
router copied to clipboard

useSearch bug: renders 'undefined' in between re-renders

Open saibs opened this issue 2 years ago • 3 comments

Describe the bug

I see is useSearch() re-renders the component on change with the following

  • new updated value
  • undefined
  • new updated value

On a full refresh with query param this works as expected

  • new updated value

image

Your Example Website or App

https://stackblitz.com/edit/tanstack-router-5lbt2p?file=src%2Fmain.tsx

Steps to Reproduce the Bug or Issue

From the repro stackblitz link

  • goto dashboard
  • invoice tab
  • click on one of the invoice
  • click show notes
  • check console log

Expected behavior

similar to full refresh, once the useSearch renders an updated values, it shouldn't re-render with undefined and then render again with updated value

Screenshots or Videos

No response

Platform

  • OS: [e.g. macOS, Windows, Linux]
  • Browser: [e.g. Chrome, Safari, Firefox]
  • Version: [e.g. v0.0.1-beta.266]

Additional context

link to discord discussion

saibs avatar Dec 13 '23 12:12 saibs

I'm having trouble with the same issue. To be more precise, useSearch is not returning undefined, it's returning the previous search value. In the example above, if you click on "close notes", you'll see that search first returns an empty object as expected, then returns the previous {showNotes: true}, and then again empty object. I have a case where I'm watching search and syncing local state with search, and the intermediate stale search is breaking that.

mer10z avatar Dec 15 '23 02:12 mer10z

Well that’s not good. I’ll fix this

tannerlinsley avatar Dec 15 '23 02:12 tannerlinsley

Can't replicate in latest any more, so it seems it's fixed!

tannerlinsley avatar Dec 20 '23 02:12 tannerlinsley