일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 |
- awss3
- 헤드퍼스트전략패턴
- 시스템설계면접예시
- 디자인패턴
- git commit merge
- git commit 협업
- 가상면접으로대규모시스템
- 리팩터링2판테스트
- cypress React
- react-ga
- formik react-query submitting not working
- Git commit 합치기
- 가상면접2장
- react
- 리팩토링2판4장
- 시스템설계면접팁
- 리액트구글애널리틱스
- 시스템설계
- 시스템설계방법
- gitsquash
- s3이미지다운로드됨
- file not found Error
- FirebaseAnalytics
- 테스트코드책
- cypressBDD
- formik submitting not working
- 가상면접3장
- git squash
- 시스템설계면접
- 전략패턴
- Today
- Total
mingg IT
[React] Context API 본문
리액트 프로젝트 내에 전역적으로 관리할 데이터가 있을 때 Context API를 사용한다.
예를 들면 사용자 로그인 정보, 애플리케이션 환경 설정, 테마 등
리액트 라이브러리로 리덕스, 리액트 라우터, styled-components 도 Context API를 기반으로 구현되어 있음
전역적으로 관리할 데이터는 주로 최상위 컴포넌트인 App의 state에 넣어서 관리함
예제)
마우스 클릭에 따라 색상 바뀌는것
왼쪽은 큰 정사각형, 오른쪽은 작은 정사각형 색이 변함
오른쪽 마우스 클릭은 onContextMenu를 사용! 왼쪽 마우스는 OnClick
components/ColorBox.js
import React from "react";
import { ColorConsumer } from "../contexts/color";
const ColorBox = () => {
return (
<ColorConsumer>
{(value) => (
<>
<div
style={{
width: "64px",
height: "64px",
background: value.state.color,
}}
/>
<div
style={{
width: "32px",
height: "32px",
background: value.state.subcolor,
}}
/>
</>
)}
</ColorConsumer>
);
};
export default ColorBox;
components/SelectColor.js
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;
contexts/color.js
import { createContext } from "react";
import React, { useState } from "react";
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;
App.js
import logo from "./logo.svg";
import "./App.css";
import ColorBox from "./components/ColorBox";
import { ColorProvider } from "./contexts/color";
import SelectColors from "./components/SelectColor";
function App() {
return (
<ColorProvider>
<div>
<SelectColors />
<ColorBox />
</div>
</ColorProvider>
);
}
export default App;
useContext Hook 사용
components/ColorBox.js
import React, { useContext } from "react";
import ColorContext from "../contexts/color";
import { ColorConsumer } from "../contexts/color";
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,
}}
/>
</>
);
};
export default ColorBox;
클래스형 컴포넌트로 static contextType 사용
장점 : 클래스 메서드에서도 Context에 넣어 둔 함수를 호출할 수 있다.
단점 : 한 클래스에서 하나의 Context밖에 사용하지 못한다.
useContext사용을 권장
components/SelectColor.js
import React, { Component } from "react";
import ColorContext from "../contexts/color";
const colors = ["red", "orange", "yellow", "green", "blue", "indigo", "violet"];
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>
);
}
}
export default SelectColors;
'FrontEnd' 카테고리의 다른 글
[React] 클래스형 컴포넌트와 함수형 컴포넌트 (0) | 2020.12.14 |
---|---|
[React] 웹팩(webpack) 과 create-react-app (0) | 2020.12.14 |
[React, JavaScript] 비동기 처리 (0) | 2020.12.08 |
[React] NavLink (0) | 2020.12.08 |
[React] 뒤로가기, 홈으로 가기 History 이용 (0) | 2020.12.07 |