// App.js
function App() {
return <GrandParent value="Hello World!" /> // 출발
}
function GrandParent({ value }) {
return <Parent value={value} />
}
function Parent({ value }) {
return <Child value={value} />
}
function Child({ value }) {
return <Message value={value} />
}
function Message({ value }) {
return <div>전달받은 데이터 : {value}</div> // 도착(😕)
}
export defualt App
import { createContext } from 'react'
// MyContext 객체 생성
const MyContext = createContext()
function App() {
return (
<MyContext.Provider value="Hello World!">
<GrandParent /* (X) value="Hello World!" *//>
</MyContext.Provider>
)
}
// App.js
function App() { /* 생략 */ }
function GrandParent(/* (X) { value } */) {
return <Parent /*(X) value={value} */ />
}
function Parent(/* (X) { value } */) {
return <Child /*(X) value={value} */ />
}
function Child(/* (X) { value } */) {
return <Message /*(X) value={value} */ />
}
function Message(/* (X) { value } */) {
const value = useContext(MyContext) // 👈
return <div>전달받은 데이터 : {value}</div>
}
export defualt App
import { createContext } from 'react'
export const MyContext = createContext("")
import ChildComponent from './ChildComponent'
import { MyContext } from './Context'
const ParentComponent = () => {
return (
<MyContext.Provider value="안녕하세요">
<ChildComponent />
</MyContext.Provider>
)
}
export default ParentComponent
import React, { useContext } from 'react'
import { MyContext } from './Context'
const ChildComponent = () => {
const value = useContext(MyContext)
return <div>전달받은 데이터 : {value}</div>
}
실행결과
이미지로 이해하기
const MyContext = createContext('Default Value')
기본값 지정 실습
import { createContext } from 'react'
export const MyContext = createContext("기본값 지정")
import ChildComponent from './ChildComponent'
import { MyContext } from './Context'
const ParentComponent = () => {
return (
/*(X) <MyContext.Provider value="안녕하세요"> */
<ChildComponent />
/*(X) </MyContext.Provider> */
)
}
import { createContext } from 'react'
export const ThemeContext = createContext()
function App() {
const [darkMode, setDarkMode] = useState(false)
return (
<ThemeContext.Provider value=({ darkMode, setDarkMode })>
<HomeComponent />
</ThemeContext.Provider>
)
}
import './HomeComponent.scss'
const HomeComponent = () => {
const data = useContext(ThemeContext)
console.log(data)
return (
<div className="container" >
<HeaderComponent />
<MainComponent />
<FooterComponent />
</div>
)
}
const HeaderComponent = () => {
return (
<div>
헤더
</div>
)
}
export default HeaderComponent
const MainComponent = () => {
return (
<div>
메인
</div>
)
}
export default MainComponent
const FooterComponent = () => {
return (
<div>
푸터
</div>
)
}
export default FooterComponent
* {
margin: 0;
}
.container {
width: 100%;
height: 100vh;
display: flex;
flex-direction: column;
}
@mixin flexCenter() {
display: flex;
justify-content: center;
align-items: center;
font-size: 30px;
}
.header {
@include flexCenter();
width: 100%;
height: 100px;
border-bottom: 3px solid gray;
}
.main {
@include flexCenter();
flex: 1;
}
.footer {
@include flexCenter();
width: 100%;
height: 100px;
border-top: 3px solid gray;
}
실행결과
이미지로 이해하기
const HeaderComponent = () => {
const { darkMode, setDarkMode } = useContext(ThemeContext)
const toggleDarkMode = () => {
setDarkMode(!darkMode)
}
const theme = () => {
backgroundColor : darkMode ? "black" : "white",
color: darkMode ? "white" : "black",
}
return (
<div className="header" style={theme}>
헤더
{
darkMode
? (<button className="toggleBtn" onClick={toggleDarkMode}>
😴
</button>)
: (<button className="toggleBtn" onClick={toggleDarkMode}>
🤩
</button>)
}
</div>
)
}
.toggleBtn {
background-color: transparent;
border: none;
font-size: 30px;
}
const MainComponent = () => {
const { darkMode } = useContext(ThemeContext)
const theme = {
background: darkMode ? "black" : "white",
color : darkMode ? "white" : "black",
}
return (
<div className="main" style={theme}>
메인
</div>
)
}
const FooterComponent = () => {
const { darkMode } = useContext(ThemeContext)
const theme = {
background: darkMode ? "black" : "white",
color : darkMode ? "white" : "black",
}
return (
<div className="footer" style={theme}>
footer
</div>
)
}
실행결과
이미지로 이해하기