TanStack / router

🤖 Fully typesafe Router for React (and friends) w/ built-in caching, 1st class search-param APIs, client-side cache integration and isomorphic rendering.
https://tanstack.com/router
MIT License
8.28k stars 664 forks source link

UseSearch not updating #2798

Closed master8848 closed 3 days ago

master8848 commented 1 week ago

Which project does this relate to?

Router

Describe the bug

I have a application where generic components are managing search and filter states also search and filter states are using navigation but use search is not updating.

navigate is changing url but its not rerunning useMemo when console log without use effect Appliction is not rerunning on change of searchParams

Your Example Website or App

https://stackblitz.com/edit/tanstack-router-5lj1t6?file=src%2Froutes%2Findex.tsx,src%2Froutes%2F__root.tsx&preset=node

Steps to Reproduce the Bug or Issue

    "@tanstack/router-zod-adapter": "^1.58.9",
    "@tanstack/react-router": "^1.82.0",

my on Filter/search change after debounce for search is something like

const onFilterChange = useCallback( (v: T): void | Promise => { navigate({ to: '.', search: (c) => { console.log('FilterBar.tsx:122', c) const newValue = v

                const retunning = c
                if (!retunning.q) retunning.q = {}

                Object.assign(retunning.q, newValue, {
                    page: c.q?.page ?? 1,
                    search: search || undefined,
                })
                Object.assign(retunning, newValue, {
                    page: c.q?.page ?? 1,
                    search: search || undefined,
                })

                return retunning
            },
        })
    },
    [navigate,  search],
)

for this console log "FilterBar.tsx:122" all latest values are available while console log in route page is not updating """ const params = Route.useSearch() console.log(params) """

Defination of searchValidation is done like """ export const ZodListQuerySchemaExtend = { page: fallback(number().optional(), 1).default(1), search: fallback(string().optional(), ''), order_by: fallback(string().optional(), ''),

[QUERY_STRINGS.start_date]: fallback(DATE_ZOD_VALIDATOR, undefined),
[QUERY_STRINGS.end_date]: fallback(DATE_ZOD_VALIDATOR, undefined),

} const searchSchema = z.object({ q: fallback(z.object(ZodListQuerySchemaExtend), { page: 1 }), }) export const Route = createFileRoute('')({ component: Component, validateSearch: zodSearchValidator(searchSchema), }) """

Expected behavior

console log in Component should be updated when url changes

Screenshots or Videos

No response

Platform

Additional context

No response

schiller-manuel commented 6 days ago

this works

https://stackblitz.com/edit/tanstack-router-k8whah?file=src%2Froutes%2Findex.tsx,src%2Froutes%2F__root.tsx&preset=node

master8848 commented 6 days ago

I checked recreation of nested state with different object works but could that be mentioned on use search params docs for new object needs to be created ,as react state normally re run if first level object is recreated into new object so for garbage collection saving our product uses reassign to same object reference on nested object but for this case it goes differently than react state

Also I am aware of use memo and use callback not trigging on nested items if reference of nested item is not changed

schiller-manuel commented 3 days ago

feel free to create a PR to the docs