Share
Embed
React/Next によるアプリケーション開発のこれから
at Encraft #4 React/Next.js 最前線
https://knowledgework.connpass.com/event/285601/
koba04
June 29, 2023
Transcript
34 slides
-
@koba04 • Work • Cybozu / SmartHR • OSS •
Maintainer … SWR, ja.react.dev, react-transition-group • Creator … SWRDevTools • Contributor … React, Next -
ࠓͷ༰ • ࠷ۙͷ React ͷมԽ • React Framework ͱͯ͠ͷ Next
• ͜Ε·Ͱͷߟ͑ํͷҧ͍ • Live Coding 🛠 -
վΊͯ React ͷಛ • View = Component(State) • Composable •
એݴత UI • ୯Ұͳσʔλϑϩʔ • ίϯϙʔωϯτϕʔε -
<Suspense /> • Suspense σʔλϑΣονϯάϦιʔεऔಘɺStreaming ੍ޚͷ ͨΊͷϓϦϛςΟϒ • σʔλऔಘͱϩʔσΟϯάͷؔ৺Λ •
Streaming ࣌ͷڥքͱͯ͠ͷ Suspense -
React Server Components (RSC) • Server Ͱ “͚ͩ” ࣮ߦ͞ΕΔ৽͍͠λΠϓͷίϯϙʔωϯτ =
Client Ͱ࣮ߦ͞Εͳ͍ • Server ʹࣄલϏϧυؚΉ • HTML Ͱͳ͘ ReactTree ͱͯ͠ฦ͞ΕΔͷͰΫϥΠΞϯτͰͷࠩߋ৽͕Մೳ • Server ͰͷΈར༻ՄೳͳϦιʔεΛ͑Δ • SSR ͱશ͘ผͷ֓೦ɻSSR ͱΈ߹ΘͤΔ͜ͱՄೳ • SSR ॳظϩʔυʹର͢Δ࠷దԽ • Suspense Λϕʔεͱͨ͠σʔλϑΣονϯάͷϓϦϛςΟϒ • “use client” ʹΑΔ Client Components ͱͷڥք, `server-only`, `client-only` ʹΑΔڧ੍ -
React Server Components (RSC) ※ Server Components Server ͰͷΈ࣮ߦͰ͖ΔɻClient
Ͱ Component ͕ͳ͍ͷͰ࣮ߦͰ͖ͳ͍ Server Components Client Components ⚠as Props ⚠as Props ⚠ Server Components ͷ݁ՌΛ Props ͱͯ͠ड͚औΓ Client Components ʹ͢͜ͱ͕Մೳɻ -
React Server Components ͱ SSR Request <Content /> <Content />
HTML & JavaScript Request ReactTree (ಠࣗ Protocol) <Content /> <Content /> HTML Λੜ ReactTree Λੜ <Content /> αʔόʔͱΫϥΠΞϯτͲͪΒͰಈ͘ <Content /> αʔόʔͷΈͰಈ͘ SSR React Server Components -
Data Fetching for Suspense • React Server Components ͕ Suspense
ʹ͓͚ΔσʔλϑΣονϯάͷ ιϦϡʔγϣϯ • σʔλͷऔಘ React ͷύϥμΠϜͷதͰղܾ͢Δ • σʔλ͕ඞཁͳՕॴͰσʔλϑΣονϯάΛߦ͏ • ಉ͡ϦΫΤετ Deduping (ॏෳഉআ) ͞ΕΔ • σʔλؒͰґଘ͕ؔ͋Δ߹ΥʔλʔϑΥʔϧආ͚ΒΕͳ͍ • ϨΠςϯγΛԿʹݮΒͤΔ͔ -
Waterfall Pending Page Load GET /A GET /B GET
/C Pending Pending ϒϥβʙαʔόؒͰͷ ΓͱΓʹͳΔͷͰ ϨΠςϯγ͕େ͖͘ͳΔ -
Waterfall with React Server Components Page Load Pending Pending
GET /A GET /B GET /C αʔόʙαʔόؒͰͷ ΓͱΓʹͳΔͷͰ ϨΠςϯγ͕খ͘͞ͳΔ -
You might not need Data Fetching Libraries? • ଟ͘ React
Server Components / Server Actions Ͱ࣮ݱՄೳʹ • ͜ΕΒͷϥΠϒϥϦΫϥΠΞϯτ্ͰͷιϦϡʔγϣϯʹ • ϥΠϒϥϦͷϨΠϠʔͰϢʔβʔମݧΛଥڠͤͣʹ Suspense ϕʔ εͷ Data Fetching ϥΠϒϥϦΛ࡞Δͷ͍͠ • ࠓޙɺReact Server Components ʹ͜ΕΒͷϥΠϒϥϦͰഓͬͨϓ ϥΫςΟε͕༌ೖ͞Ε͍͖ͯͦ͏ʢ༧ʣ https://tkdodo.eu/blog/you-might-not-need-react-query -
Server Action (Alpha) • Server ্Ͱ࣮ߦ͞ΕΔؔΛ form Client Components
ʹͦͷ· ·ͤΔΈ • ࣮ࡍʹΫϥΠΞϯτ͔ΒαʔόʔʹϦΫΤετ͕ൃߦ͞Εͯɺ αʔόʔ্Ͱ͕࣮ؔߦ͞ΕΔ • ͦͷͨΊɺ࣮͕ΫϥΠΞϯταΠυʹ࿙ΕΔ͜ͱͳ͍ • ϑΝΠϧ or ؔʹରͯ͠໌ࣔతʹ “use server”; Λ͚ͭΔඞཁ͕͋ Δ -
Server Action (Alpha) Server Ͱ࣮ߦ͞ΕΔؔ ΫϥΠΞϯτଆʹ৴͞Εͳ͍ Server ʹݱࡏͷ URL Ͱ
POST ͞Εͯࢦఆ͞Εͨ Action ͕࣮ߦ͞ΕΔ formAction startTransition Λͬͨํ๏Ͱ࣮ߦՄೳ -
New Hooks (Experimental) • Server Actions ͱΈ߹ΘͤΔ͜ͱͰ Optimistic Updates ૹ৴த
ͳͲͷද͕ࣔՄೳʹͳΔ • useOptimistic … Server Actions Ͱ Optimistic Updates Λ࣮ݱ • useFormStatus … ϑΥʔϜͷૹ৴ঢ়ଶͳͲΛऔಘͰ͖Δ -
React Server Components ͷ࣮ΛΔ • RSC From Scratch Part 1:
Server Components • https://github.com/reactwg/server-components/discussions/5 • React Server Components ͷίϯηϓτΛ؆қ࣮ͱͱʹઆ໌ ͍ͯ͠Δ • ⚛ Dan Abramov explores React Server Components with us! [VOD] • https://www.youtube.com/watch?v=Fctw7WjmxpU -
Next App Router • React Server Components ΛؚΉ React ͷ৽͍͠ܗΛ࣮ͨ͠ϑϨʔ
ϜϫʔΫ • React Server Components ͱ SSR Λซ༻͍ͯ͠Δ • React Server Components, Server Action, Experimental Hooks ͳͲ͕ ར༻Մೳʹͳ͍ͬͯΔ • ·ͩ·ͩΓͳ͍ػೳϓϥΫςΟεଟ͍͚Ͳ͕࣌ؒղܾͦ͠͏ • React ϑϨʔϜϫʔΫΛ௨ͯ͡͏ੈք -
࣮ͦΜͳʹͳ͍ʁ • View = Component(State) • Composable • એݴత UI
• ୯Ұͳσʔλϑϩʔ • ίϯϙʔωϯτϕʔε -
ΞϓϦέʔγϣϯશମ͕ React Tree ʹ • React Server Components ͜Ε·ͰΫϥΠΞϯτʹཹ·͍ͬͯͨ React
Tree ͷൣғΛαʔόʔʹ·Ͱ֦େͨ͠ • σʔλϑΣονϯάʹ͍ͭͯ React ͷύϥμΠϜͷதͰՄೳʹͨ͠ • αʔόʔଆͰσʔλऔಘΛߦ͏͜ͱʹΑΓΥʔλʔϑΥʔϧ Λܰݮ • σʔλϑΣονϯάΛҙࣝ͠ͳ͍ • ΫϥΠΞϯτɺαʔόʔΛ௨ͨ͡ Concurrent Rendering -
ৼΓࢠͱཐટ • Rails PHP ͷճؼͱݴΘΕΔ͚Ͳɺٕज़ཐટͷΑ͏ʹมԽ͍ͯ͠Δ • XHP GraphQL/Relay
Ͱͷ՝ײ͕ͱʹͳ͍ͬͯΔ • React ͷίϯϙʔωϯτϞσϧΛΑΓ֦ு͠ɺαʔόʔɾΫϥΠΞϯτ શମΛγʔϜϨεʹॻ͚Δ • ΫϥΠΞϯτΠϯλϥΫγϣϯͱγϯϓϧͳϦΫΤετɾϨεϙϯεϞ σϧͷ༥߹ • Suspense ʹΑΔ Streaming Ϩεϙϯε -
JavaScript ΛΘͳ͍දݱ • CSS HTML ͚ͩͰදݱͰ͖Δͷ૿͖͍͑ͯͯΔͷͰͦΕΛ ͍ͬͯ͘ • CSS
Custom Properties, CSS Container Queries • required, pattern, popover attributes • :where, :is, :has, :user-valid, :user-invalid classes • :nth-child(*** of ***), :focus-visible, :focus-within classes • <summary>, <details> elements -
ࢀߟ • The future of React with Dan Abramov &
Joe Savona (JS Party #267) |> Changelog • https://changelog.com/jsparty/267 • React Server Components: Part 1 - JSJ 582 - JavaScript Jabber - Top End Devs • https://topenddevs.com/podcasts/javascript-jabber/episodes/react-server-components-part-1- jsj-582 • React Server Components: Part 2- JSJ 583 - JavaScript Jabber - Top End Devs • https://topenddevs.com/podcasts/javascript-jabber/episodes/react-server-components-part-2- jsj-583 • React Server Components with Dan Abramov, Joe Savona, and Kent C. Dodds • https://www.youtube.com/watch?v=h7tur48JSaw • React Roundtable: Server Components, Suspense, and Actions - YouTube • https://www.youtube.com/watch?v=g5BGoLyGjY0