Skip to Content
FrontendReactContext API

1. State Management(상태관리)

14에서 쓴 Context 복습

1.1 Prop Drilling (Props로만 데이터를 전달하면 발생하는 문제)

React_14_2

Prop Drilling = Props로만 데이터를 전달하면 발생하는 문제

일반적으로 컴포넌트에게 데이터를 전달해주어야 할 때 Props를 통해 전달합니다.

jsx
1
export default function App() {
2
return <GrandParent value="Hello World!" />
3
}
4
5
function Parent({ value }) {
6
return <Child value={value} />
7
}
8
9
function Child({ value }) {
10
return <Message value={value} />
11
}
12
13
function Message({ value }) {
14
return <div>Received: {value}</div>
15
}

그런데 자식이 많은 컴포넌트에 데이터를 전달해야 하는 경우, 여러 컴포넌트를 거쳐 연달아서 Props를 설정해주어야 하기 때문에 불편하고 실수할 수 있는 가능성이 존재합니다. 이렇게 많은 자식을 가진 컴포넌트에 state값을 전달하려면 Context를 사용하면 됩니다.

Context API (리액트 기본 문법)

  • props 전송없이 state 공유 가능
  • 2가지 문제 때문에 많이 쓰이지 않음
    • state 변경시 쓸데없는 컴포넌트까지 전부 재렌더링되어 성능 이슈
    • 컴포넌트 재활용이 어려움
  • Context API보다는 redux같은 외부 라이브러리를 많이 사용
  • Official React Context Docs ✔️

2. Context API

  • 리액트 기본 문법

  • context = state 보관함

  • props 없이 state 공유할 때 사용하지만, 성능과 재활용 관점에서 문제가 있어서 많이 사용 안함

    • state 변경시 쓸데없는 컴포넌트까지 전부 재렌더링되어 성능 이슈
    • useContext()를 쓰고 있는 컴포넌트는 나중에 다른 파일에서 재사용할 때 Context를 import 하는 게 귀찮음
  • 위 문제들로 인해 라이브러리를 사용함

  • React v16.3부터 사용하기 쉽게 많이 개선됨

    • 과거에는 리액트의 Context가 굉장히 불편해서 전역 상태 관리 라이브러리를 사용했었음
    • 이제는 Context를 사용하기 편해져서 단순히 전역적인 상태를 관리하기 위함이라면 더 이상 사용해야 할 이유 X
    • 그렇지만 상태 관리 라이브러리는 상태 관리를 더욱 편하고, 효율적으로 할 수 있게 해주는 기능들을 제공
  • Redux, 리액트 라우터, styled-components 등의 라이브러리들은 Context API를 기반으로 구현되어 있음

  • 중첩해서 사용한 컴포넌트가 많을 때 편리한 문법

    • e.g. 로그인한 유저 정보, 테마(다크 테마), 선호하는 언어 등
  • Context에서 다루는 값은 꼭 전역적일 필요가 없습니다.

    • Context는 재사용성이 높은 컴포넌트를 만들 때도 매우 유용함
  • 중첩된 컴포넌트가 적으면 그냥 props쓰는 게 편함

  • c.f. Velog - 다른 사람들이 안 알려주는 리액트에서 Context API 잘 쓰는 방법 

  • cf. Velopert - Velog 리액트에서 Context API 잘 쓰는 법 


2.1 예제 1 : Context 기본 사용법

jsx
1
import { createContext, useContext } from 'react'
2
3
// 1. Context 만들기
4
// - createContext()로 Context 만들 수 있음
5
// - 기본 값을 설정하고 싶다면, createContext 함수에 인자로 기본 값을 넣어주면 됨
6
const MyContext = createContext('기본값')
7
8
export default function App() {
9
// 2. Context 객체 안에는 Provider라는 컴포넌트가 들어있음
10
// Provider 컴포넌트에 공유하고자 하는 값을 value 라는 Props로 설정하면,
11
// 자식 컴포넌트들에서 해당 값에 바로 접근 가능
12
return (
13
<>
14
<MyContext.Provider value="Hello World">
15
<Parent />
16
</MyContext.Provider>
17
<Parent />
18
</>
19
)
20
}
21
22
function Parent() {
23
return <Child />
24
}
25
26
function Child() {
27
return <Message />
28
}
29
30
function Message() {
31
// 3. Context 안에 있던 state 사용하려면, useContext() 사용
32
// - useContext() : Context 해체해주는 함수, 공유했던 모든 state가 담겨있음
33
const value = useContext(MyContext)
34
return <div>데이터 도착함!😁 : {value}</div>
35
}

2.2 예제 2 : Dark Theme

2.2.1 Context 생성

javascript
1
// Context/ThemeContext.js
2
import React from 'react'
3
4
export const themes = {
5
light: {
6
foreground: '#000000',
7
background: '#eeeeee',
8
},
9
dark: {
10
foreground: '#ffffff',
11
background: '#222222',
12
},
13
}
14
15
// createContext()로 Context 만들 수 있음
16
export const ThemeContext = React.createContext(themes.dark)

새 Context를 만들 때는 createContext 함수를 사용합니다. 파라미터에는 해당 Context의 기본 상태를 지정합니다.

  • c.f. Context를 만들 때 반드시 contexts 디렉터리에 만들 필요는 없습니다. 경로는 마음대로 정해도 됨

2.2.2 Provider

jsx
1
// Context/Example
2
import React, { Component } from 'react'
3
import ThemeButton from './ThemeButton'
4
import { ThemeContext, themes } from './ThemeContext'
5
6
export default class Example extends Component {
7
constructor(props) {
8
super(props)
9
this.state = {
10
theme: themes.light,
11
}
12
this.toggleTheme = () => {
13
this.setState(prev => ({
14
theme: prev.theme === themes.dark ? themes.light : themes.dark,
15
}))
16
}
17
}
18
render() {
19
// 📝 Context에 저장된 데이터를 사용하기 위해, Context의 Provider를 사용
20
// - 공유를 원하는 state를 value 안에 적어주면 됩니다.
21
// - ThemeContext로 감싼 모든 컴포넌트와 그 자식컴포넌트는 state를 props 전송없이 직접 사용가능
22
// 📝 데이터를 사용하려는 컴포넌트에서 Context의 Consumer를 사용 → useContext()로 대체 가능
23
return (
24
<div>
25
<ThemeContext.Provider value={this.state.theme}>
26
<ThemeButton changeTheme={this.toggleTheme} />
27
<ThemeContext.Consumer>
28
{theme => (
29
<div
30
style={{
31
height: 300,
32
width: 300,
33
backgroundColor: theme.background,
34
}}
35
></div>
36
)}
37
</ThemeContext.Consumer>
38
</ThemeContext.Provider>
39
<ThemeButton />
40
</div>
41
)
42
}
43
}

2.2.3 useContext()

jsx
1
// context/ThemeButton.jsx
2
import React, { useContext } from 'react'
3
import { ThemeContext } from './ThemeContext'
4
5
export default function ThemeButton(props) {
6
// Context 안에 있던 state 사용하려면
7
// (1) 만들어둔 Context를 import
8
// (2) useContext() 안에 넣기
9
// -- useContext()는 Context 해체해주는 함수, 공유했던 모든 state가 담겨있음
10
const theme = useContext(ThemeContext)
11
return (
12
<button
13
{...props}
14
onClick={props.changeTheme}
15
style={{ backgroundColor: theme.background, color: theme.foreground }}
16
>
17
버튼
18
</button>
19
)
20
}

App 컴포넌트에 추가

jsx
1
import Example from './components/3-10_Context/Example'
2
3
export default function App() {
4
return (
5
<>
6
<Example />
7
</>
8
)
9
}

[참고]