High Order Function - TEAM-ARK/inflearn-clone-front GitHub Wiki

์ด๋ฒˆ์— ์ด๋ฒคํŠธ ๋ฆฌ์Šค๋„ˆ์˜ ์ฝœ๋ฐฑํ•จ์ˆ˜์—๋กœ ๊ฐ’์„ ์ „๋‹ฌํ•ด์•ผ ๋ ๋•Œ HOF์œผ๋กœ๋„ ๊ฐ€๋Šฅํ•˜๋‹ค๋Š” ๊ฒƒ์„ ์•Œ์•˜๋‹ค

์ฝ”๋“œ 1-1.

// ๊ธฐ์กด ์‚ฌ์šฉ ๋ฐฉ์‹ : ์ด๋ฒคํŠธ ์ฝœ๋ฐฑํ•จ์ˆ˜์— ์ธ์ž๊ฐ€ ํ•„์š”ํ•˜๋ฉด ์ต๋ช…ํ•จ์ˆ˜ ํ˜•ํƒœ๋กœ ์ „๋‹ฌ
const onClickButton = (textList: string[], index:number, boxType: 'whatYouCanLearn' | 'expectedStudents' | 'requiredKnowledge') => {
  const textArray = [...textList];
  textArray.splice(index, 1);
  switch (boxType) {
        case 'whatYouCanLearn':
          dispatch({
            type: DELETE_ITEM_WHATYOUCANLEARN,
            data: textArray,
          });
          break;
        case 'expectedStudents':
          dispatch({
            type: DELETE_ITEM_EXPECTEDSTUDENTS,
            data: textArray,
          });
          break;
        case 'requiredKnowledge':
          dispatch({
            type: DELETE_ITEM_REQUIREDKNOWLEDGE,
            data: textArray,
          });
          break;
        default:
          console.error('boxType is wrong');
      }
}

<TextListBox key={index} onClick={() => onClickButton(data?.textlist, index)} />

์ฝ”๋“œ 1-2.

// ์ƒˆ๋กญ๊ฒŒ ์ ์šฉํ•˜๋Š” ๋ฐฉ์‹ : ํ•จ์ˆ˜๋ฅผ ๋ฆฌํ„ดํ•˜๋Š” ํ•จ์ˆ˜๋ฅผ ๋งŒ๋“ค์–ด์„œ ์ „๋‹ฌ
const onClickButton = (textList: string[], index:number, boxType: 'whatYouCanLearn' | 'expectedStudents' | 'requiredKnowledge') = () => {
  const textArray = [...textList];
  textArray.splice(index, 1);
  switch (boxType) {
        case 'whatYouCanLearn':
          dispatch({
            type: DELETE_ITEM_WHATYOUCANLEARN,
            data: textArray,
          });
          break;
        case 'expectedStudents':
          dispatch({
            type: DELETE_ITEM_EXPECTEDSTUDENTS,
            data: textArray,
          });
          break;
        case 'requiredKnowledge':
          dispatch({
            type: DELETE_ITEM_REQUIREDKNOWLEDGE,
            data: textArray,
          });
          break;
        default:
          console.error('boxType is wrong');
      }
}

<TextListBox key={index} onClick={onClickButton(data?.textlist, index)}

๊ทธ ์ „์—” anonymous ํ•จ์ˆ˜๋ฅผ ๊ทธ ์ž๋ฆฌ์—์„œ ๋งŒ๋“ค์–ด์„œ ๊ฐ’์„ ์ „๋‹ฌํ–ˆ์—ˆ๋Š”๋ฐ ๋” ์„น์‹œํ•œ ๋ฐฉ๋ฒ•์ด ์žˆ๋Š” ๊ฑธ ์•Œ๊ฒŒ ๋˜๋‹ˆ ์•ž์œผ๋กœ ์ด๋ ‡๊ฒŒ ์‚ฌ์šฉํ•ด์•ผ๊ฒ ๋‹ค๋Š” ์ƒ๊ฐ์ด ๋“ ๋‹ค

๊ทธ ์ „์— HOF๊ฐ€ ๋ฌด์—‡์ธ์ง€ ๋ช…ํ™•ํžˆ ์•Œ๊ณ ๊ฐˆ ํ•„์š”๊ฐ€ ์žˆ๋‹ค

High Order Function

  • ํ•˜๋‚˜ ์ด์ƒ์˜ ํ•จ์ˆ˜๋ฅผ ์ธ์ž๋กœ ๋ฐ›๋Š”๋‹ค.
  • ํ•จ์ˆ˜๋ฅผ ๊ฒฐ๊ณผ๋กœ ๋ฐ˜ํ™˜ํ•œ๋‹ค.

Use Case

array method

  • Array.prototype.filter(), map(), reducer()
const words = ['spray', 'limit', 'elite', 'exuberant', 'destruction', 'present'];

const result = words.filter(word => word.length > 6);

console.log(result);
  • filter()
  • ํ•„ํ„ฐ๋ง ํ•˜๋Š” ์กฐ๊ฑด์„ ํ•จ์ˆ˜๋ฅผ ํ†ตํ•ด ์ œ์–ดํ•˜๋Š” ๊ฒƒ

ramda

  • ์ฝ์–ด๋ด๋„ ์ž˜ ๋ชจ๋ฅด๊ฒ ๋‹ค.

๋‚ด๊ฐ€ ์ž‘์„ฑํ•œ ์ฝ”๋“œ๋Š” HOF ์ธ๊ฐ€?

  • ํ•จ์ˆ˜๋ฅผ ๋ฆฌํ„ดํ•˜๋Š” ๊ฒƒ์€ ๋งž์ง€๋งŒ ํ•จ์ˆ˜๋ฅผ ์ธ์ž๋กœ ๋ฐ›์ง€ ์•Š๊ธฐ ๋•Œ๋ฌธ์— HOF์ด๋ผ๊ธฐ ๋ณด๋‹จ ํ•จ์ˆ˜๋ฅผ ๋ฆฌํ„ดํ•˜๋Š” ํ•จ์ˆ˜๋ผ๊ณ  ํ‘œํ˜„ํ•˜๋Š”๊ฒŒ ๋งž๋Š” ๊ฑธ๊นŒ

  • ๊ทธ๋ž˜๋„ 1-1.๋ณด๋‹ค 1-2. ์ฝ”๋“œ๊ฐ€ ๋ฉ”๋ชจ๋ฆฌ๋ฅผ ๋” ์ ๊ฒŒ ์‚ฌ์šฉํ•  ๊ฒƒ ๊ฐ™๋‹ค.

useCallback

  • ํ•จ์ˆ˜ memoization(๋ฉ”๋ชจ์ด์ œ์ด์…˜)ํ•˜๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉ๋˜๋Š” hook ํ•จ์ˆ˜
  • ์ปดํฌ๋„ŒํŠธ ์•ˆ์— ํ•จ์ˆ˜๊ฐ€ ์„ ์–ธ์ด ๋˜์–ด ์žˆ๋‹ค๋ฉด ํ•ด๋‹น ์ปดํฌ๋„ŒํŠธ๊ฐ€ ๋ Œ๋”๋ง ๋  ๋•Œ ๋งˆ๋‹ค ์ƒˆ๋กœ์šด ํ•จ์ˆ˜๊ฐ€ ์ƒ์„ฑ ๋จ
    • course_info๊ฐ€ ํŽ˜์ด์ง€ ๊ทธ ์ž์ฒด์ด๋ฏ€๋กœ ๋ Œ๋”๋ง์ด ๋‹ค์‹œ ๋ ์ผ์€ ์—†์–ด์„œ useCallback์„ ์‚ฌ์šฉํ•ด๋„ ์•„๋ฌด๋Ÿฐ ํšจ๊ณผ๊ฐ€ ์—†์„ ๊ฒƒ ๊ฐ™๋‹ค.
      • ๊ทธ๋ž˜์„œ useCallback์œผ๋กœ ๋ฐ”๊พผ ์ฝ”๋“œ๋ฅผ ์ปค๋ฐ‹ํ•˜๋ ค๋‹ค๊ฐ€ ์˜๋ฏธ์—†๋Š” ๊ฒƒ ๊ฐ™์•„์„œ ๋ฐ˜์˜ํ•˜์ง€ ์•Š์•˜๋‹ค.

์ฐธ๊ณ  ๋ฌธํ—Œ

๋ธ”๋กœ๊ทธ ์›๋ณธ