Recoil - woowa-techcamp-2021/store-4 GitHub Wiki

Recoil

๊ณต์‹๋ฌธ์„œ, ์ฐธ๊ณ 

Recoil ํ•œ๊ธ€ ๋ฌธ์„œ https://recoiljs.org/ko/

์†Œ๊ฐœ

React๋ฅผ ์œ„ํ•œ ์ƒํƒœ๊ด€๋ฆฌ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ

์ฒ ํ•™

  1. ์ž‘๊ณ  React ์Šค๋Ÿฌ์šด

    React์˜ ๋‚ด๋ถ€์ƒํƒœ์™€ ๊ฐ™์ด get/set ์ธํ„ฐํŽ˜์ด์Šค๋กœ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋„๋ก ์ œ๊ณต

์ฃผ์š” ๊ฐœ๋…

Atoms

์ƒํƒœ์˜ ๋‹จ์œ„

๋ฆฌ์•กํŠธ์˜ state์™€ ์œ ์‚ฌ

Selectors

์ตœ์†Œํ•œ์˜ ์ƒํƒœ๋ฅผ atoms๋กœ ๊ด€๋ฆฌํ•˜๋ฉฐ ๊ทธ ์™ธ๋Š” ๋ชจ๋‘ selectors ์ˆœ์ˆ˜ํ•จ์ˆ˜๋ฅผ ํ†ตํ•ด ๊ณ„์‚ฐํ•˜์—ฌ ์‚ฌ์šฉ

  • ์“ธ๋ชจ์—†๋Š” ์ƒํƒœ์˜ ๋ณด์กด์„ ๋ฐฉ์ง€

์ปดํฌ๋„ŒํŠธ ์ž…์žฅ์—์„  atoms์™€ ๋™์ผํ•œ ์ธํ„ฐํŽ˜์ด์Šค๋กœ ๋™์ž‘

์˜ˆ์ œ

const fontSizeState = atom({
  key: 'fontSizeState',
  default: 14,
});

const fontSizeLabelState = selector({
  key: 'fontSizeLabelState',
  get: ({get}) => {
    const fontSize = get(fontSizeState);
    const unit = 'px';

    return `${fontSize}${unit}`;
  },
});

//

function FontButton() {
  const [fontSize, setFontSize] = useRecoilState(fontSizeState);
  return (
  	<div>
  		<p style={{fontSize}}>font size change</p>
			<button onClick={() => setFontSize((size) => size + 1)} style={{fontSize}}>
	        Click to Enlarge
      </button>
  	</div>
  );
}

function FontButton() {
  const [fontSize, setFontSize] = useRecoilState(fontSizeState);
  const fontSizeLabel = useRecoilValue(fontSizeLabelState);

  return (
    <>
      <div>Current font size: ${fontSizeLabel}</div>

      <button onClick={setFontSize(fontSize + 1)} style={{fontSize}}>
        Click to Enlarge
      </button>
    </>
  );
}

์žฅ์ 

  • React์™€ ์œ ์‚ฌํ•œ ์ธํ„ฐํŽ˜์ด์Šค
  • selectors์— ๋กœ์ง์„ ๋ถ„๋ฆฌ

๋‹จ์ 

  • Recoil 0.4
  • ๋ฌธ์„œ, ์ปค๋ฎค๋‹ˆํ‹ฐ
โš ๏ธ **GitHub.com Fallback** โš ๏ธ