正在加载视频...

视频加载失败

I updated Build UI's user search to use React Server Components with Next 13's appDir. The code for this was so simple.

378,375 次观看 • 3 年前 •via X (Twitter)

10 条评论

Ryan Toronto 的头像
Ryan Toronto3 年前

Here's the search component. It's a client side input that starts a transition via the onChange event. The transition adds the search term as a query param and then pushes the browser to that url:

Ryan Toronto 的头像
Ryan Toronto3 年前

Next, the server component reads the query param, performs a search, and re-renders the list of users. All this happens server-side:

Ryan Toronto 的头像
Ryan Toronto3 年前

What I find so cool about this approach: - 50ish lines of code - Fully interruptible - `isPending` to know when a search is running - No state, effects, or XHR requests - No race conditions - Sharable URL by default

Ryan Toronto 的头像
Ryan Toronto3 年前

I'm loving RSC. Makes writing a React app feel like something you'd see in PHP or Rails, but you also get the benefit of the world's best templating language 😉

Lee Robinson 的头像
Lee Robinson3 年前

@_buildui Very cool!

Jesse van der Velden 的头像
Jesse van der Velden3 年前

@_buildui Cool stuff combining the useTransition and replace from useRouter! This is the first time I see something like this 😁, or is it being used (or is there an example) somewhere else?

Ryan Toronto 的头像
Ryan Toronto3 年前

@_buildui My understanding is that Next's router and RSC are suspense based, so you can useTransition when navigating to keep old state rendered. First saw this in this example by @delba_oliveira:

Mohammad AlOulabi 的头像
Mohammad AlOulabi3 年前

@_buildui Does loading screen work in this case? Some times we want the loading state to show and thats why i use shallow routing and depend on react query

Ryan Toronto 的头像
Ryan Toronto3 年前

@_buildui Yup, loading screen works. If you were to refresh the page you'd see the normal loading screen.

Daniel Saewitz 的头像
Daniel Saewitz3 年前

@_buildui So this is all really cool. I watched your vid (ep #155). I understand it. But what happens when I want to do this in a nested component, that could be rendered anywhere – not just on a particular page/URL? Is there a "nested" layout/hidden url I can use to drive this?

相关视频