๐Ÿ“
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
  • createContext
  • Consumer
  • Provider
  • ๋™์  Context ์‚ฌ์šฉ
  • useContext Hook ์‚ฌ์šฉํ•˜๊ธฐ
  • Class Component์—์„œ static contextType ์‚ฌ์šฉํ•˜๊ธฐ
  • ์ •๋ฆฌ

Was this helpful?

  1. react

Context API

์ „์—ญ์œผ๋กœ ์‚ฌ์šฉํ•  ๋ฐ์ดํ„ฐ๊ฐ€ ์žˆ์„ ๋•Œ ์œ ์šฉํ•œ ๊ธฐ๋Šฅ

์˜ˆ์‹œ) ์‚ฌ์šฉ์ž ๋กœ๊ทธ์ธ ์ •๋ณด, ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ํ™˜๊ฒฝ ์„ค์ •, ํ…Œ๋งˆ React 16.3๋ถ€ํ„ฐ ์‚ฌ์šฉ๊ฐ€๋Šฅ

Redux, React Roter, styled-componrnts ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์—์„œ Context API ๊ธฐ๋ฐ˜์œผ๋กœ ๊ตฌํ˜„๋˜์–ด ์žˆ๋‹ค.

Redux๋‚˜ MobX ๊ฐ™์€ ์ƒํƒœ ๊ด€๋ฆฌ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ „์—ญ ์ƒํƒœ ๊ด€๋ฆฌ ์ž‘์—…์„ ๋” ํŽธ๋ฆฌํ•˜๊ฒŒ ํ•˜๊ธฐ๋„ ํ•˜์ง€๋งŒ, React v16.3 ์—…๋ฐ์ดํŠธ ์ดํ›„์—๋Š” ๋ณ„๋„์˜ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ์•Š์•„๋„ ์ „์—ญ ์ƒํƒœ๋ฅผ ์†์‰ฝ๊ฒŒ ๊ด€๋ฆฌ ํ•  ์ˆ˜ ์žˆ๋‹ค.

constexts/color.js

createContext

์ƒˆ Context๋ฅผ ๋งŒ๋“ค๋• createContext๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค.

import { createContext } from 'react';

const ColorContext = createContext({ color: 'black' });

export default ColorContext;

Consumer

ColorBox Component: ColorContext ์•ˆ์— ๋“ค์–ด ์žˆ๋Š” ์ƒ‰์ƒ์„ ๋ณด์—ฌ์คŒ props๋กœ ๋ฐ›์•„ ์˜ค๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋ผ ColorContext ์•ˆ์— ๋“ค์–ด ์žˆ๋Š” Consumer๋ผ๋Š” ์ปดํฌ๋„ŒํŠธ๋ฅผ ํ†ตํ•ด ์ƒ‰์„ ์กฐํšŒ

src/components/ColorBox.js

import React from 'react';
import ColorContext from '../contexts/color';

const ColorBox = () => {
  return (
    <ColorContext.Consumer>
      {value => (
          <div
          style={{
            width: '64px',
            height: '64px',
            background: value.color
          }}
        />
      )}
    </ColorContext.Consumer>
  )
}

export default ColorBox;

์•ˆ์— ํ•จ์ˆ˜๋ฅผ ๋„ฃ์–ด์ฃผ๋Š” ํŒจํ„ด์„ Function as a child ํ˜น์€ Render Props ๋ผ๊ณ  ํ•œ๋‹ค. ์ปดํฌ๋„ŒํŠธ์˜ children์ด ์žˆ์–ด์•ผ ํ•  ์ž๋ฆฌ์— ํ•จ์ˆ˜๋ฅผ ์ „๋‹ฌํ•œ๋‹ค.

Function as a child or Render Props

import React from 'react';

const RenderPropsSample = ({ children }) => {
  return <div>๊ฒฐ๊ณผ: {childern(5)}</div>;
};

// ๋งŒ์•ฝ ์œ„์™€ ๊ฐ™์€ ์ปดํฌ๋„ŒํŠธ๊ฐ€ ์žˆ๋‹ค๋ฉด ์ถ”ํ›„ ์‚ฌ์šฉํ•  ๋•Œ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค. 
<RenderPropsSample>{value => 2 * value}</RenderPropsSample>

children์„ ํ•จ์ˆ˜๋กœ ๋ณด๋‚ผ ์ˆ˜ ์žˆ๊ตฌ, ์ธ์ž๊ฐ’์„ ํ†ตํ•ด ์›ํ•˜๋Š” ๊ฒฐ๊ณผ๋ฅผ ๋ฐ›์„ ์ˆ˜ ์žˆ๊ตฌ๋‚˜. ํ—?!

children์„ ํ•จ์ˆ˜๋ผ ์ƒ๊ฐํ•˜๋‹ˆ ์ดํ•ดํ•˜๊ธฐ ์‰ฌ์›Œ์กŒ๋Œœ.

Provider

Context์˜ value๊ฐ’์„ ๋ณ€๊ฒฝํ•  ์ˆ˜ ์žˆ๋‹ค.

function App() {
  return (
    <ColorContext.Provider value={{ color: "red" }}>
      <div>
        <ColorBox />
      </div>
    </ColorContext.Provider>
  );
}

const ColorContext = createContext({ color: "black" }); Provider๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ์•Š์„ ๋•Œ ๊ธฐ๋ณธ๊ฐ’

Provider๋ฅผ ์‚ฌ์šฉํ•  ๋•Œ๋Š” value๋ฅผ ๊ผญ ๋ช…์‹œํ•ด์ค˜์•ผ ํ•œ๋‹ค.

๋™์  Context ์‚ฌ์šฉ

contexts/color.js ์ˆ˜์ •

import React, { createContext, useState } from "react";

// ์ƒˆ Context๋ฅผ ๋งŒ๋“ค๋• createContext๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค.
const ColorContext = createContext({
  state: { color: "black", subcolor: "red" },
  actions: {
    setColor: () => {},
    setSubcolor: () => {}
  }
});

const ColorProvider = ({ children }) => {
  const [color, setColor] = useState("black");
  const [subColor, setSubColor] = useState("red");

  const value = {
    state: { color, subColor },
    actions: { setColor, setSubColor }
  };

  return (
    <ColorContext.Provider value={value}>{children}</ColorContext.Provider>
  );
};

const { Consumer: ColorConsumer } = ColorContext;

export { ColorProvider, ColorConsumer };

export default ColorContext;

ColorProvider ์ปดํฌ๋„ŒํŠธ๋ฅผ ๋งŒ๋“ค๊ณ , ๊ทธ ์ปดํฌ๋„ŒํŠธ๋Š” ColorContext.Provider๋ฅผ ๋ Œ๋”๋งํ•œ๋‹ค.

App.js์— ColorProvider์‚ฌ์šฉ

import { ColorProvider } from "./contexts/color";

function App() {
  return (
    <ColorProvider>
      <div>
        <ColorBox />
      </div>
    </ColorProvider>
  );
}

export default App;

ColorBox ์ปดํฌ๋„ŒํŠธ์— ColorConsumer ์‚ฌ์šฉ

ํ”„๋ ˆ์  ํ…Œ์ด์…˜ ์ปดํฌ๋„ŒํŠธ์ด๋‹ˆ Consumer์‚ฌ์šฉ

import ColorConsumer from "../contexts/color";

const ColorBox = () => {
  return (
    <ColorConsumer>
      {({ state }) => (
        <>
          <div
            style={{
              width: "64px",
              height: "64px",
              background: state.color
            }}
          />
          <div
            style={{
              width: "32px",
              height: "32px",
              background: state.subcolor
            }}
          />
        </>
      )}
    </ColorConsumer>
  );
};

๊ฐ์ฒด ๋น„๊ตฌ์กฐํ™” ํ™œ๋‹น ๋ฌธ๋ฒ•์„ ์‚ฌ์šฉํ•˜์—ฌ value์—์„œ state๋งŒ ์กฐํšŒ

selectBox ์ถ”๊ฐ€

import React from "react";
import { ColorConsumer } from "../contexts/color";

const colors = ["red", "orange", "yellow", "green", "blue", "indigo", "violet"];

const SelectColors = () => {
  return (
    <div>
      <h2>์ƒ‰์ƒ์„ ์„ ํƒํ•˜์„ธ์š”.</h2>
      <ColorConsumer>
      {({ actions }) => (
      <div style={{ display: "flex" }}>
        {colors.map(color => (
          <div
            key={color}
            style={{
              background: color,
              width: "24px",
              height: "24px",
              cursor: "pointer"
            }}
            onClick={() => actions.setColor(color)}
            onContextMenu={e => {
              e.preventDefault(); // ๋งˆ์šฐ์Šค ์˜ค๋ฅธ์ชฝ ํด๋ฆญ์‹œ ๋ฉ”๋‰ด๊ฐ€ ๋œจ๋Š” ๊ฒƒ์„ ๋ฌด์‹œํ•œ๋‹ค.
              actions.setSubcolor(color);
            }}
          />
        ))}
      </div>
      )}
      </ColorConsumer>
      <hr />
    </div>
  );
};

export default SelectColors;
  actions: {
    setColor: () => {},
    setSubcolor: () => {}
  }

์ฒ˜์Œ์— ์ด ๋ถ€๋ถ„์ด ์ดํ•ด๊ฐ€ ์•ˆ๊ฐ”๋Š”๋ฐ, ๊ทธ๋ƒฅ ์ด ์ž๋ฆฌ์— ํ•จ์ˆ˜๊ฐ€ ์˜ฌ ๊ฑฐ๋ผ๋Š” ๋œป์ด์˜€๋‹ค.

onContextMenu()๋Š” ๋งˆ์šฐ์Šค ์˜ค๋ฅธ์ชฝ ํด๋ฆญํ–ˆ์„๋•Œ ์‚ฌ์šฉ

useContext Hook ์‚ฌ์šฉํ•˜๊ธฐ

context๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ์ฝ”๋“œ์—์„œ

<ColorConsumer>
  {({ state }) => (
    // code
  )}
</ColorConsumer>

ColorConsumer ์ปดํฌ๋„ŒํŠธ๋“ค ์ง€์šฐ๊ณ 

const ColorBox = () => {
  const { state } = useContext(ColorContext);
  return (
    <>
      <div
        style={{
          width: "64px",
          height: "64px",
          background: state.color
        }}
      />
      <div
        style={{
          width: "32px",
          height: "32px",
          background: state.subcolor
        }}
      />
    </>
  );
};

์•„์ฃผ ๊ฐ€๋ณ๊ณ  ๊ฐ„๋‹จํ•˜๊ฒŒ ์‚ฌ์šฉ๊ฐ€๋Šฅ

Class Component์—์„œ static contextType ์‚ฌ์šฉํ•˜๊ธฐ

class SelectColors extends Component {
  static contextType = ColorContext;

  handleSetColor = color => {
    this.context.actions.setColor(color);
  };

  handleSetSubcolor = subcolor => {
    this.context.actions.setSubColor(subcolor);
  };

  render() {
    return (
      <div>
        <h2>์ƒ‰์ƒ์„ ์„ ํƒํ•˜์„ธ์š”.</h2>
        <div style={{ display: "flex" }}>
          {colors.map(color => (
            <div
              key={color}
              style={{
                background: color,
                width: "24px",
                height: "24px",
                cursor: "pointer"
              }}
              onClick={() => this.handleSetColor(color)}
              onContextMenu={e => {
                e.preventDefault(); // ๋งˆ์šฐ์Šค ์˜ค๋ฅธ์ชฝ ํด๋ฆญ์‹œ ๋ฉ”๋‰ด๊ฐ€ ๋œจ๋Š” ๊ฒƒ์„ ๋ฌด์‹œํ•œ๋‹ค.
                this.handleSetSubcolor(color);
              }}
            />
          ))}
        </div>
        <hr />
      </div>
    );
  }
}
 static contextType = ColorContext;

Class Component์—์„œ๋„ static contextType์„ ์ด์šฉํ•˜์—ฌ Context๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์ง€๋งŒ ๋‹จ์ ์€ ํ•œ class ์ปดํฌ๋„ŒํŠธ๋‹น ํ•˜๋‚˜์˜ Context๋ฐ–์— ์‚ฌ์šฉ๋ชปํ•œ๋‹ค.. ๐Ÿ˜•

์ •๋ฆฌ

๋‹จ์ˆœํ•œ ์ „์—ญ ์ƒํƒœ ๊ด€๋ฆฌ๋Š” ๋ฆฌ๋•์Šค ๋Œ€์‹ ์— Context API๋กœ ๋Œ€์ฒด ํ•  ์ˆ˜ ์žˆ๋‹ค. ํ•˜์ง€๋งŒ ๋ฆฌ๋•์Šค๋Š” ๋”์šฑ ํ–ฅ์ƒ๋œ ์„ฑ๋Šฅ๊ณผ ๋ฏธ๋“ค์›จ์–ด ๊ธฐ๋Šฅ, ๊ฐ•๋ ฅํ•œ ๊ฐœ๋ฐœ์ž ๋„๊ตฌ, ์ฝ”๋“œ์˜ ๋†’์€ ์œ ์ง€ ๋ณด์ˆ˜์„ฑ์„ ์ œ๊ณต..

PreviousreduxNextํ•จ์ˆ˜ํ˜• ์ปดํฌ๋„ŒํŠธ์™€ ํด๋ž˜์Šค, ์–ด๋–ค ์ฐจ์ด๊ฐ€ ์กด์žฌํ• ๊นŒ?

Last updated 5 years ago

Was this helpful?