Upgrade to Pro — share decks privately, control downloads, hide ads and more …

React/Next によるアプリケーション開発のこれから

React/Next によるアプリケーション開発のこれから

at Encraft #4 React/Next.js 最前線
https://knowledgework.connpass.com/event/285601/

koba04

June 29, 2023
Tweet

More Decks by koba04

Other Decks in Programming

Transcript

  1. @koba04 • Work • Cybozu / SmartHR • OSS •

    Maintainer … SWR, ja.react.dev, react-transition-group • Creator … SWRDevTools • Contributor … React, Next
  2. ࠓ೔ͷ಺༰ • ࠷ۙͷ React ͷมԽ • React Framework ͱͯ͠ͷ Next

    • ͜Ε·Ͱͷߟ͑ํͷҧ͍ • Live Coding 🛠
  3. վΊͯ React ͷಛ௃ • View = Component(State) • Composable •

    એݴత UI • ୯Ұͳσʔλϑϩʔ • ίϯϙʔωϯτϕʔε
  4. React Server Components (RSC) • Server Ͱ “͚ͩ” ࣮ߦ͞ΕΔ৽͍͠λΠϓͷίϯϙʔωϯτ =

    Client Ͱ࣮ߦ͞Εͳ͍ • Server ʹ͸ࣄલϏϧυ΋ؚΉ • HTML Ͱ͸ͳ͘ ReactTree ͱͯ͠ฦ͞ΕΔͷͰΫϥΠΞϯτͰͷࠩ෼ߋ৽͕Մೳ • Server ͰͷΈར༻ՄೳͳϦιʔεΛ࢖͑Δ • SSR ͱ͸શ͘ผͷ֓೦ɻSSR ͱ૊Έ߹ΘͤΔ͜ͱ΋Մೳ • SSR ͸ॳظϩʔυʹର͢Δ࠷దԽ • Suspense Λϕʔεͱͨ͠σʔλϑΣονϯάͷϓϦϛςΟϒ • “use client” ʹΑΔ Client Components ͱͷڥք, `server-only`, `client-only` ʹΑΔڧ੍
  5. React Server Components (RSC) ※ Server Components ͸ Server ͰͷΈ࣮ߦͰ͖ΔɻClient

    Ͱ͸ Component ͕ͳ͍ͷͰ࣮ߦͰ͖ͳ͍ Server Components Client Components ⚠as Props ⚠as Props ⚠ Server Components ͷ݁ՌΛ Props ͱͯ͠ड͚औΓ Client Components ʹ౉͢͜ͱ͕Մೳɻ
  6. React Server Components ͱ SSR Request <Content /> <Content />

    HTML & JavaScript Request ReactTree (ಠࣗ Protocol) <Content /> <Content /> HTML Λੜ੒ ReactTree Λੜ੒ <Content /> ͸αʔόʔͱΫϥΠΞϯτͲͪΒͰ΋ಈ͘ <Content /> ͸αʔόʔͷΈͰಈ͘ SSR React Server Components
  7. Data Fetching for Suspense • React Server Components ͕ Suspense

    ʹ͓͚ΔσʔλϑΣονϯάͷ ιϦϡʔγϣϯ • σʔλͷऔಘ΋ React ͷύϥμΠϜͷதͰղܾ͢Δ • σʔλ͕ඞཁͳՕॴͰσʔλϑΣονϯάΛߦ͏ • ಉ͡ϦΫΤετ͸ Deduping (ॏෳഉআ) ͞ΕΔ • σʔλؒͰґଘؔ܎͕͋Δ৔߹΢ΥʔλʔϑΥʔϧ͸ආ͚ΒΕͳ͍ • ϨΠςϯγΛ೗ԿʹݮΒͤΔ͔
  8. Waterfall ໰୊ Pending Page Load GET /A GET /B GET

    /C Pending Pending ϒϥ΢βʙαʔόؒͰͷ ΍ΓͱΓʹͳΔͷͰ ϨΠςϯγ͕େ͖͘ͳΔ
  9. Waterfall ໰୊ with React Server Components Page Load Pending Pending

    GET /A GET /B GET /C αʔόʙαʔόؒͰͷ ΍ΓͱΓʹͳΔͷͰ ϨΠςϯγ͕খ͘͞ͳΔ
  10. 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
  11. Server Action (Alpha) • Server ্Ͱ࣮ߦ͞ΕΔؔ਺Λ form ΍ Client Components

    ʹͦͷ· ·౉ͤΔ࢓૊Έ • ࣮ࡍʹ͸ΫϥΠΞϯτ͔ΒαʔόʔʹϦΫΤετ͕ൃߦ͞Εͯɺ αʔόʔ্Ͱؔ਺͕࣮ߦ͞ΕΔ • ͦͷͨΊɺ࣮૷͕ΫϥΠΞϯταΠυʹ࿙ΕΔ͜ͱ͸ͳ͍ • ϑΝΠϧ or ؔ਺ʹରͯ͠໌ࣔతʹ “use server”; Λ͚ͭΔඞཁ͕͋ Δ
  12. Server Action (Alpha) Server Ͱ࣮ߦ͞ΕΔؔ਺ ΫϥΠΞϯτଆʹ͸഑৴͞Εͳ͍ Server ʹݱࡏͷ URL Ͱ

    POST ͞Εͯࢦఆ͞Εͨ Action ͕࣮ߦ͞ΕΔ formAction ΍ startTransition Λ࢖ͬͨํ๏Ͱ΋࣮ߦՄೳ
  13. New Hooks (Experimental) • Server Actions ͱ૊Έ߹ΘͤΔ͜ͱͰ Optimistic Updates ΍ૹ৴த

    ͳͲͷද͕ࣔՄೳʹͳΔ • useOptimistic … Server Actions Ͱ Optimistic Updates Λ࣮ݱ • useFormStatus … ϑΥʔϜͷૹ৴ঢ়ଶͳͲΛऔಘͰ͖Δ
  14. 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
  15. Next App Router • React Server Components ΛؚΉ React ͷ৽͍͠ܗΛ࣮૷ͨ͠ϑϨʔ

    ϜϫʔΫ • React Server Components ͱ SSR Λซ༻͍ͯ͠Δ • React Server Components, Server Action, Experimental Hooks ͳͲ͕ ར༻Մೳʹͳ͍ͬͯΔ • ·ͩ·ͩ଍Γͳ͍ػೳ΍ϓϥΫςΟε͸ଟ͍͚Ͳ͕࣌ؒղܾͦ͠͏ • React ͸ϑϨʔϜϫʔΫΛ௨ͯ͡࢖͏ੈք
  16. ΞϓϦέʔγϣϯશମ͕ React Tree ʹ • React Server Components ͸͜Ε·ͰΫϥΠΞϯτʹཹ·͍ͬͯͨ React

    Tree ͷൣғΛαʔόʔʹ·Ͱ֦େͨ͠ • σʔλϑΣονϯάʹ͍ͭͯ΋ React ͷύϥμΠϜͷதͰՄೳʹͨ͠ • αʔόʔଆͰσʔλऔಘΛߦ͏͜ͱʹΑΓ΢ΥʔλʔϑΥʔϧ໰୊ Λܰݮ • ΋͸΍σʔλϑΣονϯάΛҙࣝ͠ͳ͍ • ΫϥΠΞϯτɺαʔόʔΛ௨ͨ͡ Concurrent Rendering
  17. ৼΓࢠͱཐટ • Rails ΍ PHP ͷճؼͱ΋ݴΘΕΔ͚Ͳɺٕज़͸ཐટͷΑ͏ʹมԽ͍ͯ͠Δ • XHP ΍ GraphQL/Relay

    Ͱͷ՝୊ײ͕΋ͱʹͳ͍ͬͯΔ • React ͷίϯϙʔωϯτϞσϧΛΑΓ֦ு͠ɺαʔόʔɾΫϥΠΞϯτ શମΛγʔϜϨεʹॻ͚Δ • ΫϥΠΞϯτΠϯλϥΫγϣϯͱγϯϓϧͳϦΫΤετɾϨεϙϯεϞ σϧͷ༥߹ • Suspense ʹΑΔ Streaming Ϩεϙϯε
  18. 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
  19. ࢀߟ • 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