Teruhisa Yamamoto

Teruhisa Yamamoto

Software Developer / Father / Enthusiast of fishing and beer

Let's say Hi, in Tokyo or Discord.

What's your dev style?

My tweets

Server functions are called by POST with multipart/form-data in Next.js <form action={...}> or <form><button formAction={...} /></form>. ACTION_ID is a reference of the server function as an API endpoint.

Image
4
Reply

React Tip: You know we can use a key to trigger rerendering components when we change the key, right? You don't need to use the useEffect inside <Content /> to change content by a key, like a user id. Just do it.

Image
14
Reply

Let's dive into React Server Components!! First, How does it work? What is the difference between normal components? When render and commit in streaming? ...What's RSC? (I need your public review🙃)

Rendering flow of React Server Components
1. Server just returns a pure index.html with React, and then begins rendering components
2. Async components wrapped in <Suspense /> are awaited for like data fetching, and then their components are rendered as JSON.
3. Client commits the rendered VDOM received from the Server via streaming as soon as it arrives
258
Reply

Recently, the topic of React Server Components has become more prevalent. I took the time to review React concepts like SPA, data fetching and client-side rendering, SSR, and Suspense.

How does React work as a basic Single Page Application?
1. Send a request for a page
a. The browser displays a blank page
2. Receive all necessary files, such as HTML, JS, and CSS, and begin rendering the page
b1. The browser displays the received HTML file (e.g. <div id="root"></div>)
b2. React builds the whole page view as a VDOM (document tree in JS)
b3. React applies the VDOM to the root tag to draw the view for users
3. Display the complete pageFetching data with rendering flow
1. Fetch data to render components
a. Server prepares requested data
2. Received data, then rendering components
b. Replace loading view with the rendered components
3. Display the componentsServer Side Rendering / Static Site Generation
1. Prepare the data needed to build the requested page
2. Render the actual HTML with the data, then send the built HTML along with JavaScript to the client
3. Display the HTML first, then load required JS/CSS files
4. Render the VDOM on the client to make the elements interactive, which is also known as "hydration"Streaming HTML(SSR) + Selective Hydration in React 18
1. Build HTML containing some components that use <Suspense />, and then respond to it using renderToPipeableStream
2. Display the received HTML containing some components with a "Loading..." indicator, and then begin loading the remaining components
3. Render the requested components and then send the response back to the client
4. Hydration
285
Reply

React Server Components の前に知っておいたほうが良さそうなStreaming HTMLとSelective Hydration。 Data fetchやcode splitしたいコンポーネントをSuspenseで囲んでおいて初期表示を速める(SSRという点はこれまでと変わらず)。残りの2,3,4もSSRなんだけど並列化可能。 全てSSRという点に注目。

Image
76
Reply

React Server Components 理解のための SSR / SSG の復習。 serverではリクエストページ”全体”のHTMLを構築します。 1と2はcacheやprebuildで短縮可能です。3,4でのJS読み込み〜hydration完了でようやくbuttonやinputが動作可能になることも押さえておきましょう。 そして1~4全てがwaterfallです。

Image
11
Reply

React Server Components 理解のための復習。 ”Render-As-You-Fetch”, "Fetch-on-Render" と呼ばれるパターンです。 コンポーネントで必要なデータを随時取得していくので、データ取得が逐次的になりやすいですね。並列化は実装者次第。 不要なJSXが初回リクエスト時に取得済みな事もポイント。

Fetching data with rendering flow in React
59
Reply
Replying to @t6adev

課題はserver/client間の型とバリデーション。真実とすべきはserverなのでreq.bodyのバリデーションをzodで、後はres.send部分をreturnで書く🎫 あとは"type"をexportしてclientで使えば、clientでバリデーションすることも二重で型定義することも不要。 もちろん補完も効く。

Image
12
Reply

TypeScriptでタイプガード書きまくるの辛いし不要な物が交じる可能性もあるので完璧ではない。 そんなときのzod先生。スキーマ定義したらパーサーも型も手に入って不純物は取り除いてくれる。惚れる。 const user: unknown のところを User でキャストする悪い子も素直になれるはず。

43
Reply

これを見て自分の中の靄が晴れてきた感じ。 Jotai / Recoil は従来のReduxのようなトップダウンな状態管理ライブラリではなくボトムアップ型。 なので一元的にまとめるような構成は哲学に反してる気がする。 Single source of truth 、巨大なstoreから切り出すのを忘れよう? zenn.dev/8_8/articles/5…

Image
140
Reply

いやぁ、駆け出しエンジニアの皆さんも大困惑のNextjs v13ですな😂 React始めるのに際してCRAの代わりにNext使う流れになってきてこれだし、チュートリアル終わったらノーマルすっ飛ばしてハードモードって感じ。 日本語記事を頼りにするしか無い人達は2歩も3歩も出遅れる・・ beta.nextjs.org/docs/rendering…

86
Reply