T
TanStack5mo ago
conscious-sapphire

Input box dynamically changing search params with out losing focus

My current implementation is causing a re-render of my input box on each key I type into the input box causing me to lose focus on that input box. Is there a pattern I can use to keep focus but still update my search params as I type.
15 Replies
genetic-orange
genetic-orange5mo ago
please provide a complete minimal example
conscious-sapphire
conscious-sapphireOP5mo ago
const searchSearchSchema = z.object({
searchTerm: z.string().catch(''),
});

export const Route = createFileRoute('/search')({
component: Search,
validateSearch: zodValidator(searchSearchSchema),
search: {
middlewares: [stripSearchParams(searchSearchSchema.parse({}))],
},
});

export function Search() {

const { searchTerm } = Route.useSearch();

return (
<input
value={searchTerm}
onChange={(e) => {
navigate({
to: '.',
search: { searchTerm: e.target.value },
});
}
/>
);
}
const searchSearchSchema = z.object({
searchTerm: z.string().catch(''),
});

export const Route = createFileRoute('/search')({
component: Search,
validateSearch: zodValidator(searchSearchSchema),
search: {
middlewares: [stripSearchParams(searchSearchSchema.parse({}))],
},
});

export function Search() {

const { searchTerm } = Route.useSearch();

return (
<input
value={searchTerm}
onChange={(e) => {
navigate({
to: '.',
search: { searchTerm: e.target.value },
});
}
/>
);
}
Let me know if it's not clear from the example. Thanks in advance 🙏
genetic-orange
genetic-orange5mo ago
cant reproduce
genetic-orange
genetic-orange5mo ago
genetic-orange
genetic-orange5mo ago
doesnt lose focus
sensitive-blue
sensitive-blue5mo ago
Is using the navigate function here going rerender the whole app, because it's happing for me
genetic-orange
genetic-orange5mo ago
where?
sensitive-blue
sensitive-blue5mo ago
I mean, If I am using navigate({to: ".", search: ...}) on input change, is that supposed to rerender the whole app?
genetic-orange
genetic-orange5mo ago
no it will rerender all components that are subscriber to router state that changes during said navigation
sensitive-blue
sensitive-blue5mo ago
How do I know if a component is subscribing to the state ? e.x the pagination is an isolated component but its being rerendered, I guess the main Route is doing that, that's why childrens are rerendering too.
genetic-orange
genetic-orange5mo ago
are you using some hook from router there? like useRouterState
sensitive-blue
sensitive-blue5mo ago
Nope, I added a debounce mechanism now, do you think the complete rerender is weird, I mean since the url is change it makes sens to rerender the parent Route ?
genetic-orange
genetic-orange5mo ago
give me a reproducer , otherwise no idea
sensitive-blue
sensitive-blue5mo ago
Oh, I think I found why, beacuse Im using Route.useLoaderData(); and that's why the whole Route is rerendering
deep-jade
deep-jade3mo ago
I have a same problem

Did you find this page helpful?