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λ‘ λ체 ν μ μλ€. νμ§λ§ 리λμ€λ λμ± ν₯μλ μ±λ₯κ³Ό λ―Έλ€μ¨μ΄ κΈ°λ₯, κ°λ ₯ν κ°λ°μ λꡬ, μ½λμ λμ μ μ§ λ³΄μμ±μ μ 곡..
Last updated
Was this helpful?