import {useState} from "react";
const A = () => {
const [value, setValue] = useState("아직 안바뀜");
const handleOnClick = (e) => {
setValue("E의 값을 변경");
};
return (
<div>
<B value={value}/>
<button onClick={handleOnClick}>E의 값을 바꾸기</button>
</div>
);
};
const B = ({value}) => {
return (
<div>
<p>여긴 B</p>
<C value={value}/>
</div>
);
}
const C = ({value}) => {
return (
<div>
<p>여긴 C</p>
<D value={value}/>
</div>
);
}
const D = ({value}) => {
return (
<div>
<p>여긴 D</p>
<E value={value}/>
</div>
);
}
const E = ({value}) => {
return (
<div>
<p>여긴 E</p>
<h3>{value}</h3>
</div>
);
}
export default A;
import {useState} from "react";
const A = () => {
const [value, setValue] = useState("아직 안바뀜");
return(
<div>
<p>{value}</p>
<B setValue={setValue}/>
</div>
);
};
const B = ({setValue}) => {
return(
<div>
<p>여긴 B</p>
<C setValue={setValue} />
</div>
)
}
const C = ({setValue}) => {
return(
<div>
<p>여긴 C</p>
<D setValue={setValue}/>
</div>
)
}
const D = ({setValue}) => {
return(
<div>
<p>여긴 D</p>
<E setValue={setValue}/>
</div>
)
}
const E = ({setValue}) => {
const handleOnClick = () => {
setValue("A 의 값을 변경");
}
return(
<div>
<p>여긴 E</p>
<button onClick={handleOnClick}>클릭</button>
</div>
)
}
export default A;
// context를 생성한다.
import {createContext} from "react";
const PersonContext = createContext();
export default PersonContext;
index.js
import React from 'react';
import ReactDOM from 'react-dom/client';
import './index.css';
import App from './App';
import reportWebVitals from './reportWebVitals';
import PersonContext from "./contexts/PersonContext";
const root = ReactDOM.createRoot(document.getElementById('root'));
// 최상위 컴포넌트를 Context로 쌓아서 넣어준다.
const persons = [
{id: 0, name: "Mark", age: 30},
{id: 1, name: "Hanna", age: 28}
];
// value를 해당 객체를 너헝준다.
root.render(
<React.StrictMode>
<PersonContext.Provider value={persons}>
<App />
</PersonContext.Provider>
</React.StrictMode>
);
// If you want to start measuring performance in your app, pass a function
// to log results (for example: reportWebVitals(console.log))
// or send to an analytics endpoint. Learn more: https://bit.ly/CRA-vitals
reportWebVitals();
// 컨텍스트를 가져온다
import PersonContext from "../contexts/PersonContext";
const Example1 = () => {
// 컨텍스트 컨슈머를 사용한다.
return(
<PersonContext.Consumer>
// value를 사용
{(persons) => (<ul>
{persons.map((person, idx) => (
<li key={idx}>{person.name}</li>
))}
</ul>)}
</PersonContext.Consumer>
);
}
export default Example1;
import React from "react";
import PersonContext from "../contexts/PersonContext";
export default class Example2 extends React.Component{
// 여러개를 지정할 수 없음
// static contextType 컨텍스트를 설정
//static contextType = PersonContext;
render() {
// 사용한다.
const persons = this.context;
return(
<ul>
{persons.map((person, idx) => (
<li key={idx}>{person.name}</li>
))}
</ul>
);
}
}
Example2.contextType = PersonContext;
import PersonContext from "../contexts/PersonContext";
import {useContext} from "react";
const Example3 = () => {
// Hook으로 컨텍스트를 가져온다.
const persons = useContext(PersonContext);
// 사용한다.
return(
<ul>
{persons.map((person, idx) => (
<li key={idx}>{person.name}</li>
))}
</ul>
);
}
export default Example3;