๐Ÿ“
til
  • TIL(Today I Learned)
  • javascript
    • value-number-string-boolean-null-undefined
    • primitive-reference
    • Hoisting
    • Prototypes in Javascript
    • this
    • prototype
    • ์ฝœ๋ฐฑํ•จ์ˆ˜ (Callback function)
    • ํ•จ์ˆ˜์˜ ํ˜ธ์ถœ
    • ์ž๋ฃŒ๊ตฌ์กฐ new keyword
    • closure
    • Promise
    • event-loop
    • array-object
    • ๊ฐ์ฒด
    • Arguments
    • higher order function
    • operators-function-control-flow
    • ๊ฐ์ฒด ์ƒ์„ฑ ํŒจํ„ด 3๊ฐ€์ง€
    • Javascript scopes
    • Functional Programming
    • Design Patterns
    • ๋ฐ์ดํ„ฐ ํƒ€์ž…
    • Object ๊ฐ์ฒด
    • ํ‘œ์ค€ ๋‚ด์žฅ ๊ฐ์ฒด์˜ ํ™•์žฅ
    • ์ฐธ์กฐ
    • ํ•จ์ˆ˜
    • ์ƒ์†(Inheritance)
    • this - 'this'๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ํ•ด๋‹น ํ•จ์ˆ˜๋ฅผ '์–ด๋–ป๊ฒŒ' ์‹คํ–‰ํ•˜๋А๋ƒ์— ๋”ฐ๋ฅธ 4๊ฐ€์ง€ this ์ •์˜
    • ์ „์—ญ๊ฐ์ฒด(Global object)
    • ๊ฐ์ฒด ์ง€ํ–ฅ ํ”„๋กœ๊ทธ๋ž˜๋ฐ
    • The 'new' keyword - Object Creation in JavaScript
  • javascript-api
    • Number
      • Number.MAX_VALUE
      • Number.isInteger
      • Number.NEGATIVE_INFINITY
      • Number.isNaN()
      • Number.POSITIVE_INFINITY
      • Number.parseFloat
      • Number.EPSILON
      • number.toExponential
      • Number.MAX_SAFE_INTEGER
      • Number1 - ์ž์—ฐ์ˆ˜, ์ •์ˆ˜, 10์ง„์ˆ˜, 2์ง„์ˆ˜, ๋ถ€๋™์†Œ์ˆ˜์ , ์‹ค์ˆ˜
      • Number.isSafeInteger()
      • Number.MIN_VALUE
      • Number.parseInt
      • Number.NaN
      • Number.isFinite()
      • Number.MIN_SAFE_INTEGER
      • toFixed
    • string.split
    • String.fromCodePoint
    • string.trimEnd
    • string.padStart
    • string.@@iterator
    • String.fromCharCode
    • string.toUpperCase
    • string.codePointAt
    • string.toLowerCase()
    • string.toString
    • string.includes
    • string.replace()
    • string.charAt
    • String.lastIndexOf
    • string.slice
    • string.search
    • string.padEnd
    • string.substring
    • string.length
    • string.trim
    • string.localeCompare
    • String.indexOf
    • string.endsWith
    • string.valueOf
    • String.raw
    • string.matchAll()
    • string.repeat
    • string.match
    • String.prototype
    • string.startsWith
    • string.charCodeAt
    • string.trimStart
    • string.concat
    • string.toLocaleUpperCase()
    • string.toLocaleLowerCase
    • String
  • learn-node
    • debugger
    • Tip
  • schema-design
    • Database Schema Design
    • Database Schema Design
  • react
    • LifeCycle
    • redux
    • Context API
    • ํ•จ์ˆ˜ํ˜• ์ปดํฌ๋„ŒํŠธ์™€ ํด๋ž˜์Šค, ์–ด๋–ค ์ฐจ์ด๊ฐ€ ์กด์žฌํ• ๊นŒ?
    • Hooks๊ณผ useEffect ์‚ฌ์šฉํ•ด ๋ณด๊ธฐ
    • Route
    • async wait ์‚ฌ์šฉํ•˜๊ธฐ
    • Hooks API Reference
    • context
    • npm uninstall ํ•˜๋Š”๋ฒ•
    • test ๋งŒ๋“ค๊ธฐ
  • tip
    • ํด๋ฆฐ์ฝ”๋“œ
    • BxSlider๋กœ ํ…์ŠคํŠธ ํ๋ฅด๋Š” ํšจ๊ณผ ๋งŒ๋“ค๊ธฐ
  • javascript30
    • Event Capture, Propagation, Bubbling and Once
    • Object and Arrays - Reference VS Copy
  • typescript
    • ์šฐ์•„ํ•œ ํƒ€์ž…์Šคํฌ๋ฆฝํŠธ 2๋ถ€
    • The Basic Cheatsheet
    • TypeScript
    • Type Guards and Differnetiating Types
    • ์šฐ์•„ํ•œ ํƒ€์ž…์Šคํฌ๋ฆฝํŠธ
    • Generics
  • git-from-the-hell
    • git
    • init-status-add-commit-log-stage-repository
    • log-diff
    • ๋จธ์ง€ ํ›„ branch ์‚ญ์ œํ•˜๊ธฐ
    • ์ง€์˜ฅ์—์„œ ์˜จ Git
    • reset-revert
    • develop branch ๋ฅผ pull ํ•˜๊ณ  ์‹ถ์„๋•Œ
  • conference-and-seminar
    • ๋ชจ๋˜ ํ”„๋ก ํŠธ์—”๋“œ ๊ฐœ๋ฐœํ™˜๊ฒฝ์˜ ์ดํ•ด
  • fire-base
    • Firebase .gitignore
  • vanillacoding
    • Data Structures
    • Sorting Algorithms - part 1
    • Promise
    • class
    • 04.quiz
    • 07.event-loop
    • Design Patterns
    • OOP (Object Oriented Programming)
  • etc
    • ์•Œ๊ณ ๋ฆฌ์ฆ˜ ์ฝ”๋“œ๋ฆฌ๋ทฐ
    • ์Šค์Šค๋กœ ๊ณต๋ถ€ํ•˜๋Š” ๋ฒ•
    • ๋ฐ”๋‹๋ผ์ฝ”๋”ฉ ์ˆ˜๊ฐ• ํ›„๊ธฐ
    • async ๊ณผ์ œ ํ›„๊ธฐ - ๋น„๋™๊ธฐ, ๋™๊ธฐ, ํด๋กœ์ €, ๋ฐฐ์—ด๊ณผ ๊ฐ์ฒด
    • ์œ ์šฉํ•œ ์‚ฌ์ดํŠธ
  • algorithm
    • The Supermarket Queue
    • Find the odd int
    • The Office III - Broken Photocopier
    • Directions Reduction
    • The Office II - Boredom Score
    • Divisible Sum Pairs
    • Codewars ์ด์šฉ์ž ์†”๋ฃจ์…˜ ๋ชจ์Œ
    • Shortest Word
    • find key
    • Two Sum
    • Simple Pig Latin
  • Book
    • the essence of object-orientation
      • ํƒ€์ž…๊ณผ ์ถ”์ƒํ™”
      • ๊ฐ์ฒด ์ง€๋„
      • ์ด์ƒํ•œ ๋‚˜๋ผ์˜ ๊ฐ์ฒด
      • ์ถ”์ƒํ™” ๊ธฐ๋ฒ•
      • 05. ์ฑ…์ž„๊ณผ ๋ฉ”์‹œ์ง€
      • 07.ํ•จ๊ป˜ ๋ชจ์œผ๊ธฐ
      • 04. ์—ญํ• , ์ฑ…์ž„, ํ˜‘๋ ฅ
      • ํ˜‘๋ ฅํ•˜๋Š” ๊ฐ์ฒด๋“ค์˜ ๊ณต๋™์ฒด
  • ecma-script2015
    • Object Literal Upgrades
    • default-parameter-template-literals-arrow-functions
    • spread-operator-rest-param
    • let-const-rest-parameter-spread-operator-destructuring
  • http
    • ์›น ๋ธŒ๋ผ์šฐ์ €์— URL์„ ์ž…๋ ฅํ–ˆ์„ ๋•Œ ์–ด๋–ป๊ฒŒ ์›น ํŽ˜์ด์ง€๊ฐ€ ๋ณด์—ฌ์งˆ๊นŒ?
  • jest
    • toThrow(error?)
  • data-structures
    • Data Structures
  • express
    • express generator
    • CORS
  • css-flexible-box-layout
    • flex ํ•ด๋ฒ„๋ ธ์ง€ ๋ญ์•ผ
  • git
    • Git
  • mongodb
    • MongoDB
  • markdown
    • use-markdown
  • cmder
    • cmd ๋ช…๋ น์–ด ๋ชจ์Œ
  • debug
    • trackClicksEx function error
  • npm
    • NPM TOKEN ์„ค์ •ํ•˜๊ธฐ
  • storybook
    • Storybook
  • sort
    • Sorting Algorithms - part 1
  • javascript-koans
    • Javascript Koans ์˜ค๋‹ต๋…ธํŠธ
  • rxjs
    • Rx.js
  • dom-elements
    • HTML Element
  • redux-toolkit
    • Redux Toolkit
  • github-actions
    • GitHub Actions
  • redux-middleware
    • redux middleware
  • rest
    • rest
  • css-rendering
    • ์ฝ”๋“œ ์Šคํ”ผ์ธ  - CSS Rendering 1ํšŒ์ฐจ 2/2
    • ์ฝ”๋“œ ์Šคํ”ผ์ธ  - CSS Rendering 1ํšŒ์ฐจ 1/2
  • you-dont-know-js
    • ํƒ€์ž…
  • server
    • # shutdown local server
  • semantic-versioning
    • Semantic Versioning 2.0.0
Powered by GitBook
On this page
  • useConetxt
  • useReducer
  • useCallback
  • useMemo

Was this helpful?

  1. react

Hooks API Reference

API๋ฅผ ๋ณด๋ฉฐ ์ •๋ฆฌํ•œ ๋‚ด์šฉ, API ๊ทธ๋Œ€๋กœ์˜ ๋‚ด์šฉ

Hook์€ React 16.8์— ์ƒˆ๋กœ ์ถ”๊ฐ€๋œ ๊ธฐ๋Šฅ์ด๋‹ค. class๋ฅผ ์ž‘์„ฑํ•˜์ง€ ์•Š๊ณ ๋„ state์™€ ๋‹ค๋ฅธ react๊ธฐ๋Šฅ์„ ์“ธ ์ˆ˜ ์žˆ๋‹ค.

  • ๊ธฐ๋ณธ Hook

    • useState

    • useEffect

    • useContext

  • ์ถ”๊ฐ€ Hooks

    • useCallback

    • useMemo

    • useRef

    • useImperativeHandle

    • useLayoutEffect

    • useDebugValue

useConetxt

const value = useContext(MyContext);

context์˜ ํ˜„์žฌ ๊ฐ’์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค. context์˜ ํ˜„์žฌ ๊ฐ’์€ ํŠธ๋ฆฌ ์•ˆ์—์„œ Hook์„ ํ˜ธ์ถœํ•˜๋Š” ์ปดํฌ๋„ŒํŠธ ๊ฐ€์žฅ ๊ฐ€๊นŒ์ด์— ์žˆ๋Š”

์˜ value prop์— ์˜ํ•ด ๊ฒฐ์ •๋œ๋‹ค.

์ƒ์œ„ ์ปดํฌ๋„ŒํŠธ์—์„œ React.memo๋ฅผ ์‚ฌ์šฉํ•˜๋”๋ผ๋„ useContext๋ฅผ ์‚ฌ์šฉํ•˜๊ณ  ์žˆ๋Š” ์ปดํฌ๋„ŒํŠธ ์ž์ฒด์—์„œ๋ถ€ํ„ฐ ๋‹ค์‹œ ๋ Œ๋”๋ง๋œ๋‹ค.

useContext ์ „๋‹ฌ ์ธ์ž๋Š” context ๊ฐ์ฒด ๊ทธ ์ž์ฒด์—ฌ์•ผ ํ•œ๋‹ค.

  • useContext(MyContext) -> ๋งž์Œ

  • useContext(MyContext.Consumer) -> ํ‹€๋ฆผ

useContext๋ฅผ ํ˜ธ์ถœํ•  ์ปดํฌ๋„ŒํŠธ๋Š” context ๊ฐ’์ด ๋ณ€๊ฒฝ๋˜๋ฉด ํ•ญ์ƒ ๋ฆฌ๋ Œ๋”๋ง ๋ ๊ฒƒ์ด๋‹ค. ๋งŒ์•ฝ ์ปดํฌ๋„ŒํŠธ๋ฅผ ๋ฆฌ๋ Œ๋”๋งํ•˜๋Š” ๋น„์šฉ์ด ๋งŽ์ด ๋“ ๋‹ค๋ฉด, ๋ฉ”๋ชจ์ œ์ด์…˜์„ ์‚ฌ์šฉํ•˜์—ฌ ์ตœ์ ํ™”ํ•  ์ˆ˜ ์žˆ๋‹ค.

ํด๋ž˜์Šค์—์„œ ์“ธ ๋•Œ๋Š” static contextType = context ๋˜๋Š” <MyContext.Consumer> ์™€ ์œ ์‚ฌํ•˜๋‹ค.

useReducer

const [state, dispatch] = useReducer(reducer, initialArg, init);

useState์˜ ๋Œ€์ฒด ํ•จ์ˆ˜์ด๋‹ค. (state, action) => newState์˜ ํ˜•ํƒœ๋กœ reducer๋ฅผ ๋ฐ›๊ณ , dispatch ๋ฉ”์„œ๋“œ์™€ ์ง์˜ ํ˜•ํƒœ๋กœ ํ˜„์žฌ state๋ฅผ ๋ฐ˜ํ™˜

๋‹ค์Œ state๊ฐ€ ์ด์ „ state์— ์˜์กด์ ์ธ ๊ฒฝ์šฐ useState๋ณด๋‹ค useReducer๊ฐ€ ๋‚ซ๋‹ค. useReducer๋Š” ์ž์„ธํ•œ ์—…๋ฐ์ดํŠธ๋ฅผ ํŠธ๋ฆฌ๊ฑฐ ํ•˜๋Š” ์ปดํฌ๋„ŒํŠธ์˜ ์„ฑ๋Šฅ์„ ์ตœ์ ํ•  ์ˆ˜ ์žˆ๋Š”๋ฐ ์ฝœ๋ฐฑ ๋Œ€์‹  dispatch๋ฅผ ์ „๋‹ฌ ํ•  ์ˆ˜ ์žˆ๋‹ค.

useState๋ฅผ ์‚ฌ์šฉํ•  ๊ฒฝ์šฐ

function Counter({initialState} {
    const [count, setCount] = useState(initialCount);
  return (
      <>
        Count: {count}
      <button onClick={() => setCount(intialCount)}>Reset</button>
          <button onClick={() => setCount(prevCount => prevCount - 1)}>+</button>
          <button onClick={() => setCount(prevCount => prevCount + 1)}></button>
    </>
  )
})

setState์˜ ์ฒซ๋ฒˆ์งธ์ธ์ž๊ฐ€ prevState ์˜€๋‹ค.. ๐Ÿ˜…

useReducer๋กœ ๋ฐ”๊ฟจ์„ ๋–„

const initialState = { count: 0 };

function reducer(state, action) {
  switch (action.type) {
    case 'increment': 
      return { count: state.count + 1 };
    case 'decrement':
      return { count: state.count - 1 };
    default:
      throw new Error();
  }
}

function Counter() {
    const [state, dispatch] = useReducer(reducer, initialState);
  return (
      <>
        Count: {state.count}
            <button onClick={() => dispatch({type: 'increment'})}>+</button>
            <buttton onClick={() => dispatch({type: 'decrement'})}>-</buttton>
    </>
  )
}

dispatch๋Š” ๋ฆฌ๋ Œ๋”๋ง ์‹œ์—๋„ ๋ณ€๊ฒฝ๋˜์ง€ ์•Š์Œ์„ ๋ณด์žฅํ•œ๋‹ค.

reducer๊ฐ€ ์ˆœ์ˆ˜ํ•จ์ˆ˜์ธ๊ฑฐ์™€ ๊ด€๋ จ์ด ์žˆ์ง€ ์•Š์„๊นŒ? ๊ทธ๋ž˜์„œ useEffect๋‚˜ useCallback ์˜์กด์„ฑ ๋ชฉ๋ก์— ์ด ํ•จ์ˆ˜๋ฅผ ํฌํ•จํ•˜์ง€ ์•Š์•„๋„ ๊ดœ์ฐฎ๋‹ค.

์ดˆ๊ธฐํ™”ํ•˜๋Š”๋ฒ• useReducer(reducer, initial);

๋‘๋ฒˆ์งธ ์ธ์ž๋กœ ์„ค์ •ํ•œ๋‹ค.

์ดˆ๊ธฐํ™”๋ฅผ ์ง€์—ฐํ•  ์ˆ˜ ์žˆ๋‹ค.

function init(initialCount) {
  return { count: initialCount };
}
function reducer(state, action) {
  switch(action.type) {
      // ...code
    case 'reset':
      return init(action.payload);
    default:
      throw new Error();
  }
}
function Counter({initialCount}) {
  const [state, dispatch] = useReducer(reducer, initialCount, init);
  return (
      <>
        Count: {state.count}
            <button onClick={() => dispatch({typ: 'reset', payload: initalCount})}></button>
            // ...code
        </>
  )
}

useReducer์˜ 3๋ฒˆ์งธ ์ธ์ž๋ฅผ ์ด์šฉํ•˜์—ฌ ์ง€์—ฐ์‹œํ‚ฌ ์ˆ˜ ์žˆ๋‹ค.

state๋ฅผ ์ดˆ๊ธฐํ™”ํ• ๋•Œ๋„ ์œ ์šฉํ•˜๋‹ค.

useCallback

const memoizedCallback = useCallback(
    () => {
    doSomething(a, b);
  },
  [a, b],
);

๋ถˆํ•„์š”ํ•œ ๋ Œ๋”๋ง์„ ๋ฐฉ์ง€ํ•ด์ค€๋‹ค. ์ฝœ๋ฐฑ์•ˆ์—์„œ ์ฐธ์กฐ๋˜๋Š” ๋ชจ๋“  ๊ฐ’์€ ์˜์กด์„ฑ ๊ฐ’์˜ ๋ฐฐ์—ด์— ๋‚˜ํƒ€๋‚˜์•ผ ํ•œ๋‹ค.

์˜์กด์„ฑ ๊ฐ’์˜ ๋ฐฐ์—ด์ด ์ฝœ๋ฐฑ์— ์ธ์ž๋กœ ์ „๋‹ฌ๋˜์ง€ ์•Š๋Š”๋‹ค.

useMemo

const memoizedValue = useMemo(() => computeExpensiveValue(a, b), [a, b]);

"์ƒ์„ฑ(create)" ํ•จ์ˆ˜์™€ ์˜์กด์„ฑ ๊ฐ’์„ ๋ฐฐ์—ด์— ์ „๋‹ฌ useMemo๋กœ ์ „๋‹ฌํ•œ ํ•จ์ˆ˜๋Š” ๋ Œ๋”๋ง ์ค‘์— ์‹คํ–‰๋œ๋‹ค.

์‚ฌ์ด๋“œ ์ดํŽ™ํŠธ(side effects)๋Š” useEffect์—์„œ ํ•˜๋Š” ์ผ์ด์ง€ useMemo์—์„œ ํ•˜๋Š” ์ผ์ด ์•„๋‹ˆ๋‹ค. ๋˜ํ•œ useMemo๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ์•Š๊ณ ๋„ ๋™์ž‘ํ•  ์ˆ˜ ์žˆ๋„๋ก ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•˜๊ณ  ๊ทธ๊ฒƒ์„ ์ถ”๊ฐ€ํ•˜์—ฌ ์„ฑ๋Šฅ์„ ์ตœ์ ํ™” ํ•˜์„ธ์š”.

Previousasync wait ์‚ฌ์šฉํ•˜๊ธฐNextcontext

Last updated 5 years ago

Was this helpful?