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. 06/29/2023
    React / Next ʹΑΔ
    ΞϓϦέʔγϣϯ։ൃͷ͜Ε͔Β
    Encraft#4 / @koba04

    View full-size slide

  2. @koba04
    • Work

    • Cybozu / SmartHR

    • OSS

    • Maintainer … SWR, ja.react.dev, react-transition-group

    • Creator … SWRDevTools

    • Contributor … React, Next

    View full-size slide

  3. ja.react.dev
    • ຋༁ਐΊ͍ͯΔͷͰνΣοΫ͍ͯͩ͘͠͞ʂ

    View full-size slide

  4. ࠓ೔ͷ಺༰
    • ࠷ۙͷ React ͷมԽ

    • React Framework ͱͯ͠ͷ Next

    • ͜Ε·Ͱͷߟ͑ํͷҧ͍

    • Live Coding 🛠

    View full-size slide

  5. ͜Ε·Ͱͷ React

    View full-size slide

  6. վΊͯ React ͷಛ௃
    • View = Component(State)

    • Composable

    • એݴత UI

    • ୯Ұͳσʔλϑϩʔ

    • ίϯϙʔωϯτϕʔε

    View full-size slide

  7. ࠷ۙͷ React ͷมԽ

    View full-size slide


  8. • Suspense ͸σʔλϑΣονϯά΍ϦιʔεऔಘɺStreaming ੍ޚͷ
    ͨΊͷϓϦϛςΟϒ

    • σʔλऔಘͱϩʔσΟϯάͷؔ৺Λ෼཭

    • Streaming ࣌ͷڥքͱͯ͠ͷ Suspense

    View full-size slide

  9. React Server Components (RSC)
    • Server Ͱ “͚ͩ” ࣮ߦ͞ΕΔ৽͍͠λΠϓͷίϯϙʔωϯτ = Client Ͱ࣮ߦ͞Εͳ͍

    • Server ʹ͸ࣄલϏϧυ΋ؚΉ

    • HTML Ͱ͸ͳ͘ ReactTree ͱͯ͠ฦ͞ΕΔͷͰΫϥΠΞϯτͰͷࠩ෼ߋ৽͕Մೳ

    • Server ͰͷΈར༻ՄೳͳϦιʔεΛ࢖͑Δ

    • SSR ͱ͸શ͘ผͷ֓೦ɻSSR ͱ૊Έ߹ΘͤΔ͜ͱ΋Մೳ

    • SSR ͸ॳظϩʔυʹର͢Δ࠷దԽ

    • Suspense Λϕʔεͱͨ͠σʔλϑΣονϯάͷϓϦϛςΟϒ

    • “use client” ʹΑΔ Client Components ͱͷڥք, `server-only`, `client-only` ʹΑΔڧ੍

    View full-size slide

  10. React Server Components (RSC)
    ※ Server Components ͸ Server ͰͷΈ࣮ߦͰ͖ΔɻClient Ͱ͸ Component ͕ͳ͍ͷͰ࣮ߦͰ͖ͳ͍
    Server Components
    Client Components
    ⚠as Props ⚠as Props


    Server Components ͷ݁ՌΛ

    Props ͱͯ͠ड͚औΓ

    Client Components ʹ౉͢͜ͱ͕Մೳɻ

    View full-size slide

  11. Server Components and Client Components
    https://nextjs.org/docs/getting-started/react-essentials#when-to-use-server-and-client-components

    View full-size slide

  12. React Server Components ͱ SSR
    Request


    HTML & JavaScript
    Request
    ReactTree (ಠࣗ Protocol)


    HTML


    Λੜ੒
    ReactTree


    Λੜ੒
    ͸αʔόʔͱΫϥΠΞϯτͲͪΒͰ΋ಈ͘
    ͸αʔόʔͷΈͰಈ͘
    SSR
    React Server Components

    View full-size slide

  13. Data Fetching for Suspense
    • React Server Components ͕ Suspense ʹ͓͚ΔσʔλϑΣονϯάͷ
    ιϦϡʔγϣϯ

    • σʔλͷऔಘ΋ React ͷύϥμΠϜͷதͰղܾ͢Δ

    • σʔλ͕ඞཁͳՕॴͰσʔλϑΣονϯάΛߦ͏

    • ಉ͡ϦΫΤετ͸ Deduping (ॏෳഉআ) ͞ΕΔ

    • σʔλؒͰґଘؔ܎͕͋Δ৔߹΢ΥʔλʔϑΥʔϧ͸ආ͚ΒΕͳ͍

    • ϨΠςϯγΛ೗ԿʹݮΒͤΔ͔

    View full-size slide

  14. Waterfall ໰୊
    Pending
    Page Load
    GET /A
    GET /B
    GET /C
    Pending
    Pending
    ϒϥ΢βʙαʔόؒͰͷ


    ΍ΓͱΓʹͳΔͷͰ


    ϨΠςϯγ͕େ͖͘ͳΔ

    View full-size slide

  15. Waterfall ໰୊ with React Server Components
    Page Load
    Pending
    Pending
    GET /A
    GET /B
    GET /C
    αʔόʙαʔόؒͰͷ


    ΍ΓͱΓʹͳΔͷͰ


    ϨΠςϯγ͕খ͘͞ͳΔ

    View full-size slide

  16. 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

    View full-size slide

  17. Server Action (Alpha)
    • Server ্Ͱ࣮ߦ͞ΕΔؔ਺Λ form ΍ Client Components ʹͦͷ·
    ·౉ͤΔ࢓૊Έ

    • ࣮ࡍʹ͸ΫϥΠΞϯτ͔ΒαʔόʔʹϦΫΤετ͕ൃߦ͞Εͯɺ
    αʔόʔ্Ͱؔ਺͕࣮ߦ͞ΕΔ

    • ͦͷͨΊɺ࣮૷͕ΫϥΠΞϯταΠυʹ࿙ΕΔ͜ͱ͸ͳ͍

    • ϑΝΠϧ or ؔ਺ʹରͯ͠໌ࣔతʹ “use server”; Λ͚ͭΔඞཁ͕͋
    Δ

    View full-size slide

  18. Server Action (Alpha)
    Server Ͱ࣮ߦ͞ΕΔؔ਺

    ΫϥΠΞϯτଆʹ͸഑৴͞Εͳ͍
    Server ʹݱࡏͷ URL Ͱ POST ͞Εͯࢦఆ͞Εͨ Action ͕࣮ߦ͞ΕΔ

    formAction ΍ startTransition Λ࢖ͬͨํ๏Ͱ΋࣮ߦՄೳ

    View full-size slide

  19. New Hooks (Experimental)
    • Server Actions ͱ૊Έ߹ΘͤΔ͜ͱͰ Optimistic Updates ΍ૹ৴த
    ͳͲͷද͕ࣔՄೳʹͳΔ

    • useOptimistic … Server Actions Ͱ Optimistic Updates Λ࣮ݱ

    • useFormStatus … ϑΥʔϜͷૹ৴ঢ়ଶͳͲΛऔಘͰ͖Δ

    View full-size slide

  20. 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

    View full-size slide

  21. Next ͷ App Router

    View full-size slide

  22. Next App Router
    • React Server Components ΛؚΉ React ͷ৽͍͠ܗΛ࣮૷ͨ͠ϑϨʔ
    ϜϫʔΫ

    • React Server Components ͱ SSR Λซ༻͍ͯ͠Δ

    • React Server Components, Server Action, Experimental Hooks ͳͲ͕
    ར༻Մೳʹͳ͍ͬͯΔ

    • ·ͩ·ͩ଍Γͳ͍ػೳ΍ϓϥΫςΟε͸ଟ͍͚Ͳ͕࣌ؒղܾͦ͠͏

    • React ͸ϑϨʔϜϫʔΫΛ௨ͯ͡࢖͏ੈք

    View full-size slide

  23. ϑϨʔϜϫʔΫͱ React
    https://ja.react.dev/learn/start-a-new-react-project https://ja.react.dev/blog/2023/05/03/react-canaries

    View full-size slide

  24. ͜Ε·Ͱͷߟ͑ํͷҧ͍

    View full-size slide

  25. ࣮͸ͦΜͳʹͳ͍ʁ
    • View = Component(State)

    • Composable

    • એݴత UI

    • ୯Ұͳσʔλϑϩʔ

    • ίϯϙʔωϯτϕʔε

    View full-size slide

  26. ΞϓϦέʔγϣϯશମ͕ React Tree ʹ
    • React Server Components ͸͜Ε·ͰΫϥΠΞϯτʹཹ·͍ͬͯͨ
    React Tree ͷൣғΛαʔόʔʹ·Ͱ֦େͨ͠

    • σʔλϑΣονϯάʹ͍ͭͯ΋ React ͷύϥμΠϜͷதͰՄೳʹͨ͠

    • αʔόʔଆͰσʔλऔಘΛߦ͏͜ͱʹΑΓ΢ΥʔλʔϑΥʔϧ໰୊
    Λܰݮ

    • ΋͸΍σʔλϑΣονϯάΛҙࣝ͠ͳ͍

    • ΫϥΠΞϯτɺαʔόʔΛ௨ͨ͡ Concurrent Rendering

    View full-size slide

  27. ΞϓϦέʔγϣϯશମ͕ React Tree ʹ
    Data
    ͜Ε·Ͱ
    Client Server

    View full-size slide

  28. ΞϓϦέʔγϣϯશମ͕ React Tree ʹ
    Component and Data
    ͜Ε͔Β
    Client Server

    View full-size slide

  29. ৼΓࢠͱཐટ
    • Rails ΍ PHP ͷճؼͱ΋ݴΘΕΔ͚Ͳɺٕज़͸ཐટͷΑ͏ʹมԽ͍ͯ͠Δ

    • XHP ΍ GraphQL/Relay Ͱͷ՝୊ײ͕΋ͱʹͳ͍ͬͯΔ

    • React ͷίϯϙʔωϯτϞσϧΛΑΓ֦ு͠ɺαʔόʔɾΫϥΠΞϯτ
    શମΛγʔϜϨεʹॻ͚Δ

    • ΫϥΠΞϯτΠϯλϥΫγϣϯͱγϯϓϧͳϦΫΤετɾϨεϙϯεϞ
    σϧͷ༥߹

    • Suspense ʹΑΔ Streaming Ϩεϙϯε

    View full-size slide

  30. 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

    • , elements

    View full-size slide

  31. Live Coding 🛠

    View full-size slide

  32. ࢀߟ
    • 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

    View full-size slide