正在加载视频...

视频加载失败

URL state is underrated. Here's an example of how we built Next.js commerce with almost zero `useState` calls.

1,113,882 次观看 • 2 年前 •via X (Twitter)

10 条评论

Dominik 🔮 的头像
Dominik 🔮2 年前

@nextjs url state is great, but just setting `defaultValue` on the search input will not work if you use the browser back button. The url will change, the component re-renders and you'll se the correct search result, but the value in the search input is out of sync:

Lee Robinson 的头像
Lee Robinson2 年前

@nextjs Fixed! Added a key to the input.

Pablo Bermejo 的头像
Pablo Bermejo2 年前

@nextjs Am I missing something? I mean, is this new? This has been always the right pattern when you want to preserve state on refresh. Right?

John Rush 的头像
John Rush2 年前

@nextjs That’s actually old good web we’ve been doing back in 2010

Tom Lienard 的头像
Tom Lienard2 年前

@nextjs Don't forget to wrap components that uses `useSearchParams()` in a `Suspense` boundary, otherwise, your whole page might be sent to the client

Ethan Niser 的头像
Ethan Niser2 年前

@nextjs any plans to expose the internal router events for external libraries to tie into search params? was possible in pages, but no longer which is really sad because its super cool

Lee Robinson 的头像
Lee Robinson2 年前

@nextjs Have you seen this?

Lance Armah-Abraham 的头像
Lance Armah-Abraham2 年前

@nextjs It's something I thought of using and trying to implement for multi step forms, but it would mean that the already filled in (mostly non sensitive data) will be in the url (for client and server data synchronizations). And that's what I am not sure about in terms of security.

Lee Robinson 的头像
Lee Robinson2 年前

@nextjs Here's how I'd recommend handling forms!

Will4_U || Damian 的头像
Will4_U || Damian2 年前

@nextjs it's 2023, and React developers found out that URL state exists instead of throwing everything into useState that's wild

相关视频