英文:
NextJS server fetch based on client input
问题
I've put together a Client and a Server component (based on App Router
), and managed to refresh Server on user input (specifically when the user types anything), basically forcing a refresh of the route.
router.push(pathname)
So far so good, but I actually need to pass user input from Client to Server in order to filter fetch based on it (i.e a date range).
I'm guessing I could use dynamic routes, but semantically doesn't feel right (it's not like I'm navigating to an entity detail). Also, server actions kind of looks what I need, but since it's an experimental feature, I was wondering if there's already a different approach available; Server fetch based on user input seems like a basic feature to me, but still can't find a straight "official" pattern to get it working.
Any alternatives / suggestions?
EDIT
As @ivanatias suggested, this thread shows how to pass params through query strings, while this could potentially work, unfortunately since the url is changing, the state of the input fields is lost.
英文:
I've put together a Client and a Server component (based on App Router
), and managed to refresh Server on user input (specifically when the user types anything), basically forcing a refresh of the route.-
router.push(pathname)
So far so good, but I actually need to pass user input from Client to Server in order to filter fetch based on it (i.e a date range).
I'm guessing I could use dynamic routes, but semantically doesn't feel right (it's not like I'm navigating to an entity detail). Also, server actions kind of looks what I need, but since it's an experimental feature, I was wondering if there's already a different approach available; Server fetch based on user input seems like a basic feature to me, but still can't find a straight "official" pattern to get it working.
Any alternatives / suggestions?
EDIT
As @ivanatias suggested, this thread shows how to pass params through query strings, while this could potentially work, unfortunately since the url is changing, the state of the input fields is lost.
答案1
得分: 3
你可以使用更新 searchParams
的方法,在 Next.js 13 文档的示例中(app 目录)。
-
这个想法是使用客户端钩子的组合(
usePathname
、useSearchParams
和useRouter
),这些钩子从next/navigation
中导入。
英文:
You can use updating searchParams
example in Next.js 13 doc (app Dir).
-
the idea is to use a combination of client hooks (
usePathname
,useSearchParams
, anduseRouter
) where imports fromnext/navigation
.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论