为什么我们应该使用服务器端组件来从事件处理程序中获益?

huangapple go评论65阅读模式
英文:

Why should we use server side components to benefit from event handlers?

问题

在Next.js 13中,为什么我们要使用客户端组件来能够使用事件处理程序,比如onClick?我认为最终服务器端组件会被渲染成静态HTML,所以为什么我们不能从服务器端组件中返回一个<button onclick="console.log(123)">Print123</button>,因为它将被渲染成静态HTML呢?

英文:

In Nextjs 13, why should we use client-side components to be able to use event handlers such as the onClick? I believe that at-end a server-side component will be rendered into a static HTML right?

so why can't we return a <button onclick="console.log(123)">Print123</button> from a server-side component as it will be rendered into a static HTML?

答案1

得分: 1

简短版本

一个服务器端组件假设所有的变化和活动都将在服务器端发生。

换句话说,服务器端组件不仅仅是静态的,它们是惰性的,但可以包含客户端组件。

好的,那么'为什么呢'?

一个React应用程序在屏幕上预渲染了大部分(如果不是全部)的内容,然后通过将它们连接到事件和JavaScript来“激活”动态部分。

在服务器端,如果指定了任何JavaScript或事件,渲染器应该尝试将它们连接起来,运行所有内容,并返回结果的静态形式。某种程度上,React专家们现在正在对着屏幕尖叫。

所以服务器端渲染看到onChange=alert('woot')并将其解释为“将这个JavaScript连接到一个不存在的事件并...😢!”,然后出错。

那么目的是什么?

任何耗时、计算成本高昂或者你可以构建一次,保存并重复使用直到有变化的事情都可以在服务器端完成,这样你就不必依赖于最终用户的TI83级笔记本电脑来执行它。

比如,你的页面需要多次访问数据库来构建一个页面。你可以让客户端代码来访问API,但是用户可能会很讨厌,你不希望他们接近你漂亮干净的服务器。

(还有一些关于保持加载时间和带宽使用量的内容)

所以我们有一个客户端组件来访问数据库API,可能是在与数据库在同一房间的服务器上。然后它预先构建自身的静态部分,并将数据传递给需要的客户端组件。

英文:

Short version

A server-side component assumes that all the changes and activity will occur server-side.

To put it another way, server-side components are not just static, they are inert, but can contain client-side components.

Ok, so 'why?'.

A react app pre-renders most if not all the stuff on screen, then 'hydrates' the dynamic bits by hooking them up to the events and JavaScript powering them.

Server-side, if any JavaScript or events are specified, the renderer should try to hook them up, run everything, and return a static form of the result. Sorta, React wizards are screaming at the screen right now.

So the server-side rendering sees onChange=alert('woot') and reads it as "Hook this JavaScript up to an event that doesn't exist and...😨!", and chokes.

What's the point then?

Anything that is time consuming, computationally expensive, or that you could build once, save and reuse again until something changes can be done server-side, so you're not relying upon the end-users TI83 level laptop to do it.

Say, your page needs to hit the database a dozen times to build a page. You could have the client-side code smash the API, but user's are icky and you don't want them near your nice clean servers.

(also something about keeping loading times, and bandwidth usage down)

So we have a client-side component hit the database API instead, from a server that might be in the same room as the DB. Then it pre-builds the static bits of itself, and hands the data off to client-side components that need it.

huangapple
  • 本文由 发表于 2023年7月4日 23:18:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/76613988.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定