리액트
2021.03.07 / React - Hooks useContext
개발지혜
2021. 3. 7. 00:33
useContext
이 hook을 사용하면 함수형 컴포넌트에서 Context를 보다 쉽게 사용할 수 있다.
여기서 Context는 주로 전역적으로 데이터가 사용돼야 할 때 사용된다.
ContextSample.js
import React, { createContext, useContext } from 'react';
const ThemeContext = createContext('black');
const ContextSample = () => {
const theme = useContext(ThemeContext);
const style = {
width: '24px',
height: '24px',
background: theme
};
return <div style={style} />;
};
export default ContextSample;
참고