๐ŸŽ‰Floodgate ๋ฐœํ‘œ: React ๐ŸŒŠ์šฉ "๋” ๋งŽ์ด ๋กœ๋“œ"๊ตฌ์„ฑ ์š”์†Œ

8191 ๋‹จ์–ด reactjavascript
2๋…„ ์ด์ƒ์˜ ๊ฐœ๋ฐœ ๋์— React Floodgate์ด 1.0.0 ๋ฒ„์ „์œผ๋กœ ์ถœ์‹œ๋˜์—ˆ์Œ์„ ์•Œ๋ ค๋“œ๋ฆฝ๋‹ˆ๋‹ค!

Floodgate๋Š” React.js์— ๋Œ€ํ•œ "๋” ๋งŽ์€ ๋กœ๋“œ"๊ตฌ์„ฑ ์š”์†Œ๋กœ, ๋Œ€๊ธฐ ์ค‘์ธ ๋ฐ์ดํ„ฐ๋ฅผ ๊ด€๋ฆฌํ•˜๊ณ  ์—…๋ฐ์ดํŠธํ•˜๋Š” ๋ณต์žก์„ฑ์„ ์ทจํ•˜๊ณ  ์ด๋ฅผ ์ง๊ด€์ ์ธ ๊ฒฝํ—˜์œผ๋กœ ์ „ํ™˜ํ•ฉ๋‹ˆ๋‹ค. ๊ตฌ์„ฑ์— ๋Œ€ํ•œ ์ตœ์†Œํ•œ์˜ ์ ‘๊ทผ ๋ฐฉ์‹์„ ์ทจํ•ฉ๋‹ˆ๋‹ค. ๋‹จ ํ•˜๋‚˜์˜ ํ•„์ˆ˜ ์†Œํ’ˆ๊ณผ render prop pattern์„ ์‚ฌ์šฉํ•˜์—ฌ ๊ฐœ๋ฐœ์ž๋Š” ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•œ ๋ฐ์ดํ„ฐ์˜ ์–‘๊ณผ ๋ Œ๋”๋ง ๋ฐฉ์‹์„ ์ •๋ฐ€ํ•˜๊ฒŒ ์ œ์–ดํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

Floodgate์˜ ๊ธฐ๋ณธ์ ์ธ ๊ตฌํ˜„์„ ๋น ๋ฅด๊ฒŒ ์‚ดํŽด๋ณด๋ ค๋ฉด ์•„๋ž˜ ์˜ˆ๋ฅผ ํ™•์ธํ•˜์„ธ์š”.

import React from "react";
import Floodgate from "react-floodgate";

export default function LoadMore() {
  const albums = ["For Emma, Forever Ago", "Bon Iver, Bon Iver", "22 a million", "i,i"];
  return (
    <Floodgate data={albums} initial={1} increment={1}>
      {({ items, loadNext, loadComplete }) => (
        <React.Fragment>
          <h1>Bon Iver Albums</h1>
          <ol>
            {items.map(album => <li>{album}</li>)}
          </ol>
          <button onClick={loadNext} disabled={loadComplete}>Add Album</button>
        </React.Fragment>
      )}
    </Floodgate>
  );
}

์ด๊ฒƒ์ด ์ž‘๋™ํ•˜๋Š” "๋” ๋กœ๋“œ"๊ตฌ์„ฑ ์š”์†Œ๋ฅผ ๋งŒ๋“œ๋Š” ๋ฐ ํ•„์š”ํ•œ ์ „๋ถ€์ž…๋‹ˆ๋‹ค!

์ง€๊ธˆ Floodgate ์‚ฌ์šฉ์„ ์‹œ์ž‘ํ•˜๋ ค๋ฉด React ํ”„๋กœ์ ํŠธ์— ์„ค์น˜ํ•˜์„ธ์š”.

# using npm
npm i react-floodgate

# using yarn
yarn add react-floodgate

ํŠน์ง•



Floodgate๋Š” ์ตœ์†Œํ•œ์˜ ๊ธฐ๋Šฅ์ด์ง€๋งŒ ๊ทธ ๊ธฐ๋Šฅ์ด ๋‹ค๋ฅธ ๊ตฌ์„ฑ ์š”์†Œ ๋ฐ ํŒจํ„ด๊ณผ ํ•จ๊ป˜ ์–ด๋–ป๊ฒŒ ํ™œ์šฉ๋˜๋Š”์ง€์— ๋”ฐ๋ผ ์—„์ฒญ๋‚˜๊ฒŒ ๊ฐ•๋ ฅํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์•„๋ž˜์—์„œ Floodgate๊ฐ€ ํ•  ์ˆ˜ ์žˆ๋Š” ์ž‘์—…์— ๋Œ€ํ•œ ๊ฐ„๋žตํ•œ ์†Œ๊ฐœ๋ฅผ ํ™•์ธํ•˜์„ธ์š”. README file์„ ํ™•์ธํ•˜์—ฌ ๋” ๊นŠ์€ ์ดํ•ด์™€ ๊ธฐ์ˆ ์ ์ธ ์„ธ๋ถ€ ์‚ฌํ•ญ์„ ์–ป์œผ์‹ญ์‹œ์˜ค.

๐Ÿ“Š ๋ชจ๋“  ์œ ํ˜•์˜ ๋ฐ์ดํ„ฐ ์†Œ๋น„



Floodgate์˜ data ์†Œํ’ˆ์€ ๋ฐฐ์—ด์ด ์ „๋‹ฌ๋˜๊ธฐ๋งŒ ํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค. ๊ทธ ๋ฐฐ์—ด์€ ๋น„์–ด ์žˆ๋Š” ๊ฒƒ์„ ํฌํ•จํ•˜์—ฌ ๋ฌด์—‡์ด๋“  ๋  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค! ๋ฌธ์ž์—ด, ๊ตฌ๋ฌธ ๋ถ„์„๋œ JSON ๊ฐœ์ฒด, React ๊ตฌ์„ฑ ์š”์†Œ, ์‹ฌ์ง€์–ด JavaScript ํ•จ์ˆ˜๋„ ์ „๋‹ฌํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. Floodgate๋Š” ์ด๋Ÿฌํ•œ ํ•ญ๋ชฉ์˜ ๋ Œ๋”๋ง ๋ฐ ๊ตฌํ˜„์„ ๊ฐœ๋ฐœ์ž์—๊ฒŒ ๋งก๊น๋‹ˆ๋‹ค.
data={["hello", "world"]} , data={[<li>Eggs</li>, <li>cereal</li>, <li>paper towels</li>]} , data={[]} ์™€ ๊ฐ™์€ ๊ฐ’์€ ๋ชจ๋‘ Floodgate์— ์ „๋‹ฌํ•˜๊ธฐ์— ์œ ํšจํ•ฉ๋‹ˆ๋‹ค. ์ฐธ๊ณ ๋กœ, Floodgate๋Š” ์–ด๋–ค ์œ ํ˜•์˜ ํ•ญ๋ชฉ์ด ๋ฐฐ์—ด์„ ๊ตฌ์„ฑํ•˜๋Š”์ง€ ์‹ ๊ฒฝ ์“ฐ์ง€ ์•Š์ง€๋งŒ ๋ชจ๋“  ๋ฐฐ์—ด ์š”์†Œ์—์„œ ์œ ํ˜•์ด ๊ท ์ผํ•œ์ง€ ํ™•์ธํ•˜๋Š” ๊ฒƒ์ด ์ข‹์Šต๋‹ˆ๋‹ค.

๐Ÿ”ข ๋ Œ๋”๋งํ•  ํ•ญ๋ชฉ์˜ ์ˆ˜๋Ÿ‰ ๊ฒฐ์ •



Floodgate์— data ๋ฐฐ์—ด์—์„œ initial ์†Œํ’ˆ์„ ์‚ฌ์šฉํ•˜์—ฌ ์ดˆ๊ธฐ ๋ Œ๋”๋ง์— ๋กœ๋“œํ•ด์•ผ ํ•˜๋Š” ํ•ญ๋ชฉ ์ˆ˜๋ฅผ ์•Œ๋ ค์ค๋‹ˆ๋‹ค. increment prop์€ render prop ํ•จ์ˆ˜์—์„œ loadNext()์— ๋Œ€ํ•œ ํ›„์† ํ˜ธ์ถœ์—์„œ ๋กœ๋“œ๋˜๋Š” ํ•ญ๋ชฉ ์ˆ˜๋ฅผ ์ฒ˜๋ฆฌํ•ฉ๋‹ˆ๋‹ค.

๐ŸŽ› ์ƒ์œ„ ๊ตฌ์„ฑ ์š”์†Œ์—์„œ ์†Œํ’ˆ ๊ด€๋ฆฌ



React์˜ ์ˆ˜๋ช… ์ฃผ๊ธฐ ๋ฉ”์„œ๋“œ์™€ ์‚ฌ์šฉ์ž ์ •์˜ ์ฝœ๋ฐฑ ์†Œํ’ˆ์„ ํ™œ์šฉํ•˜๋ฉด Floodgate์˜ ์†Œํ’ˆ์„ ์ƒ์œ„ ๊ตฌ์„ฑ ์š”์†Œ์˜ ์ƒํƒœ๋กœ ์™„์ „ํžˆ ๊ด€๋ฆฌํ•  ์ˆ˜ ์žˆ์œผ๋ฏ€๋กœ ์ธ์Šคํ„ด์Šค์˜ ๋ฐ์ดํ„ฐ๋ฅผ ๋น„๋™๊ธฐ์‹์œผ๋กœ ์—…๋ฐ์ดํŠธํ•˜๊ฑฐ๋‚˜ ์ตœ์ข… ์‚ฌ์šฉ์ž๊ฐ€ ๋กœ๋“œ๋˜๋Š” ํ•ญ๋ชฉ ์ˆ˜๋ฅผ ๋” ์ž˜ ์ œ์–ดํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋‚˜๋Š” ์ด๊ฒƒ์„ Controlled Floodgate ํŒจํ„ด์ด๋ผ๊ณ  ๋ถ€๋ฅธ๋‹ค.

โ˜Ž๏ธ ์ด๋ฒคํŠธ ๊ธฐ๋ฐ˜ ์ฝœ๋ฐฑ ์†Œํ’ˆ์œผ๋กœ ์ด๋ฒคํŠธ ์ฒ˜๋ฆฌ



render prop ํ•จ์ˆ˜๋Š” ์ž์‹ ๊ตฌ์„ฑ ์š”์†Œ์—์„œ ํ˜ธ์ถœํ•  Floodgate ๋ฉ”์„œ๋“œ์˜ ์ˆ˜๋ฅผ ๋…ธ์ถœํ•ฉ๋‹ˆ๋‹ค. ์ฆ‰, loadNext , loadAll , reset ๋ฐ exportState ์ž…๋‹ˆ๋‹ค. ์ด๋Ÿฌํ•œ ๋ฉ”์„œ๋“œ๊ฐ€ ํ˜ธ์ถœ๋˜๋ฉด Floodgate๋Š” on[MethodName] ์†Œํ’ˆ์— ์ œ๊ณต๋œ ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•ฉ๋‹ˆ๋‹ค(์ œ๊ณต๋œ ๊ฒฝ์šฐ).

๐Ÿ”ฎ ์ปจํ…์ŠคํŠธ API ํ™œ์šฉ



Floodgate๋Š” v0.6.0๋ถ€ํ„ฐ React์˜ Context API์„ ํ™œ์šฉํ•˜๋ฏ€๋กœ ๊ฐœ๋ฐœ์ž๊ฐ€ FloodgateContext ๋‚ด๋ณด๋‚ด๊ธฐ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ํ•„์š”ํ•œ ๊ณณ์ด๋ฉด ์–ด๋””๋“ ์ง€ render prop์— ๋…ธ์ถœ๋œ ๋ฉ”์†Œ๋“œ๋ฅผ ์ „๋‹ฌํ•  ํ•„์š”๊ฐ€ ์—†์Šต๋‹ˆ๋‹ค.

๐Ÿ›  TypeScript๋กœ ์ œ์ž‘



Floodgate๋Š” Typescript๋กœ ๊ตฌ์ถ•๋˜์—ˆ์œผ๋ฉฐ ํ–ฅ์ƒ๋œ ๊ฐœ๋ฐœ์ž ๊ฒฝํ—˜์„ ์œ„ํ•ด types definition file๊ณผ ํ•จ๊ป˜ ๋ฐฐํฌ๋ฉ๋‹ˆ๋‹ค.

์˜ˆ



Floodgate๊ฐ€ ์–ด๋–ป๊ฒŒ ์ž‘๋™ํ•˜๋Š”์ง€ ๋” ์ž˜ ์ดํ•ดํ•˜๋ ค๋ฉด ๋‹ค์Œ์„ ํ™•์ธํ•˜์‹ญ์‹œ์˜คCodesandbox.io examples. ์ฝ”๋“œ๊ฐ€ ์„ค์ •๋˜๋Š” ๋ฐฉ์‹๊ณผ ์ตœ์ข… ์‚ฌ์šฉ์ž๊ฐ€ ์ƒํ˜ธ ์ž‘์šฉํ•  ๊ฒฐ๊ณผ ์•ฑ์„ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

์‚ฌ์šฉ ์ค‘์ธ Floodgate์˜ ์‹ค์ œ ์˜ˆ๋Š” ๋‚ด ๊ฐœ์ธ ์‚ฌ์ดํŠธwriting page์— ์žˆ์Šต๋‹ˆ๋‹ค.

๋กœ๋“œ๋งต



์ด ํ”„๋กœ์ ํŠธ๋Š” ๋งค์šฐ ์นœํ™˜๊ฒฝ์ ์ด์ง€๋งŒ ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•˜๊ณ  Floodgate ์‚ฌ์šฉ์ž์˜ ์ฆ‰๊ฐ์ ์ธ ์š”๊ตฌ๋ฅผ ํ•ด๊ฒฐํ•˜๋Š” ๊ฒƒ ์™ธ์—๋„ ๊ฐ€๊นŒ์šด ์žฅ๋ž˜์— ๊ตฌํ˜„ํ•  ๊ณ„ํš์ธ ๋ช‡ ๊ฐ€์ง€ ๊ธฐ๋Šฅ์ด ์žˆ์Šต๋‹ˆ๋‹ค.
  • ์˜ค๋ฅ˜ ๊ฒฝ๊ณ„
  • ํ›„ํฌ ์ง€์ง€๋Œ€ ( useFloodgate )
  • ๋ฌธ์„œ ์›น์‚ฌ์ดํŠธ
  • README, ํŠนํžˆ "๊ธฐ๊ณ ์ž"์„น์…˜ ๊ฐœ์„ 

  • GitHub์—์„œ ํƒ์ƒ‰



    Floodgate๋Š” GitHubgeoffdavis92/react-floodgate์—์„œ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์—์„œ ๊ตฌ์„ฑ ์š”์†Œ์˜ README๋ฅผ ๋ณด๊ณ , ์†Œ์Šค ํŒŒ์ผ์„ ๊ฒ€์‚ฌํ•˜๊ณ , ๋ฌธ์ œ๋ฅผ ์ œ๊ธฐํ•˜๊ณ , ์—ด๋ฆฐ ํ”„๋กœ์ ํŠธ๋ฅผ ์‚ดํŽด๋ณด๊ณ , ๋ชจ๋“  ์ผ๋ฐ˜์ ์ธ GitHub ๋ฆฌํฌ์ง€ํ† ๋ฆฌ ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

    ์–ด๋–ป๊ฒŒ ์ƒ๊ฐํ•ด?



    ํ”Œ๋Ÿฌ๋“œ๊ฒŒ์ดํŠธ ์•ฑ์ด ๋งˆ์Œ์— ๋“œ์‹œ๋‚˜์š”? ์•ฑ์— ์ด ๊ตฌ์„ฑ ์š”์†Œ๊ฐ€ ์ ˆ์‹คํžˆ ํ•„์š”ํ•ฉ๋‹ˆ๊นŒ? ๊ทธ๊ฒƒ์— ๋Œ€ํ•ด ์•Œ๋ ค์ฃผ๊ฑฐ๋‚˜ ์•„๋ž˜์— ์˜๊ฒฌ์„ ๋‚จ๊ฒจ์ฃผ์„ธ์š”!

    ๐ŸŽ‰ ํ–‰๋ณตํ•œ ๋ฐœ์ „! ๐ŸŽ‰

    ์ข‹์€ ์›นํŽ˜์ด์ง€ ์ฆ๊ฒจ์ฐพ๊ธฐ