createContext
함수를 사용해 Context 생성import {createContext} from "react";
const ColorContext=createContext({color:"black"}); //기본 상태 색상 : 검정
export default ColorContext;
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;
import './App.css';
import ColorBox from './component/ColorBox';
const App=()=>{
return (
<div>
<ColorBox/>
</div>
);
};
export default App;
import logo from './logo.svg';
import './App.css';
import ColorBox from './component/ColorBox';
import ColorContext from './contexts/color';
const App=()=>{
return (
<ColorContext.Provider value={{color:"red"}}>
{/*provider를 사용한 value 변경*/}
<div>
<ColorBox/>
</div>
</ColorContext.Provider>
);
};
export default App;
ColorProvider
컴포넌트에 ColorContext.Provider
랜더링value
에 상태(state
), 업데이트 함수(actions
) 분리해서 묶어서 전달하면 다른 컴포넌트에 Context 값 사용할 때 편함import {createContext, useState} from "react";
const ColorContext=createContext({
state:{color:"black", subcolor:"red"},
actions:{
setColor:()=>{},
setSubcolor:()=>{}
}
});
const ColortProvider=({children})=>{
const [color,setColor]=useState("black");
const [subcolor,setSubcolor]=useState("red");
const value={
state:{color,subcolor}, //상태
actions:{setColor,setSubcolor} //업데이트 함수
};
return (
//상태와 업데이트 함수 따로 분리하여 작성하여
//ColorContext.Provider에 넣는 객체의 형태와 일치 하는 것이 좋음
<ColorContext.Provider value={value}>
{children}
</ColorContext.Provider>);
};
//const ColorConsumer =ColortContext.Consumer
const {Consumer:ColorConsumer}=ColorContext;
//ColorProvider와 ColorConsumer 내보내기
export {ColortProvider,ColorConsumer};
export default ColorContext;
import './App.css';
import ColorBox from './component/ColorBox';
import { ColortProvider } from './contexts/color';
const App=()=>{
return (
<ColortProvider>
<div>
<ColorBox/>
</div>
</ColortProvider>
);
};
export default App;
import React from 'react';
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>
);
};
export default ColorBox;
import React from 'react';
const colors=["red","orange","yellow","green","blue","indigo","violet"];
const SelectColors=()=>{
return (
<div>
<h2> 색상을 선택하세요.</h2>
<div style={{display:"flex"}}>
{colors.map(color=>(
<div key={color}
style={{
background:color,
width:"24px",
height:"24px",
cursor:"pointer"
}}/>
))}
</div>
<hr/>
</div>
);
};
export default SelectColors;
import './App.css';
import ColorBox from './component/ColorBox';
import { ColortProvider } from './contexts/color';
import SelectColors from './component/SelectColors';
const App=()=>{
return (
<ColortProvider>
<div>
<SelectColors/>
<ColorBox/>
</div>
</ColortProvider>
);
};
export default App;
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;
함수형 컴포넌트에서만 사용 가능, 클래스형에서는 사용 불가능
import React, { useContext } from 'react';
import ColorContext 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;
useContext Hook 사용
import React, { Component } from 'react';
import ColorContext, { ColorConsumer } from '../contexts/color';
const colors=["red","orange","yellow","green","blue","indigo","violet"];
class SelectColors extends Component{ //클래스형으로 작성
//static contextType 지정
static contextType=ColorContext;
//this.context 조회 = 현재 context의 value 가르킴
handleSetColor=color=>{
this.context.actions.setColor(color); //setcolor 호출
};
handleSetSubcolor=subcolor=>{
this.context.actions.setSubcolor(subcolor); //setSubcolor 호출
}
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;