๋ฆฌ์กํธ ์ํ๊ด๋ฆฌ - props & Context API
1. ํด๋ผ์ด์ธํธ ์ํ๊ด๋ฆฌ (State management) ?
-
SPAs๊ตฌ์กฐ์์๋ ๋ค์ํ ํ๋ฉด ๋ ๋๋ง์ด ํ์ํจ.
-
ํ๋ฉด ๋ ๋๋ง์ ๋ค์ํ state ๋ณ๊ฒฝ์ ์ํด์ ๋ฐ์.
-
๋ค์์ state๊ฐ ์์ผ๋ฉฐ ๊ฐ state๋ ๋ณ๊ฒฝ๋ ์ ์๊ณ , ์ด๋ค state๋ ์๋ฒ์ ๋๊ธฐํ ๋ผ์ผ ํ๋ค.
2. React state management
component block state
-
ํ๋์ ์ปดํฌ๋ํธ ์์์์ ๋ณ๊ฒฝ
-
์ง์ญ๋ณ์์ ๋ณ๊ฒฝ๊ณผ ๋น์ท
-
const [data, setData] = useState("");
component sharing state
์ปดํฌ๋ํธ ๊ฐ์ ์ํ๋ฅผ ๊ณต์ ํ๋ ๊ฒฝ์ฐ๊ฐ ๋ง๊ณ , ๋๋ถ๋ถ์ global state ์์ค์ผ๋ก ๊ด๋ฆฌํ๋ค.
-
์ํ๋ฅผ ์์์๊ฒ ์ ๋ฌ
-
props ๋ก ์ ๋ฌ
-
์์ ์ปดํฌ๋ํธ๋ props์์ฑ์ ํตํด์ ํ์ํ ์ํ๋ฅผ ๋ฐ์์ ๋ ๋๋ง
-
-
์ํ๋ฅผ ์์์ด ์๋ ๋ค๋ฅธ node ๊ณ์ธต์ ์ ๋ฌ
-
์์ ์ด๋๊ฐ state๋ฅผ ๋ง๋ค์ด์ ์ปดํฌ๋ํธ๊ฐ์ ๊ณต์ ๊ฐ ๋๋๋ก ํ๊ธฐ.
-
์๋ ๊ทธ๋ฆผ์ฒ๋ผ ๊ณตํต์ ๋ถ๋ชจ ์์ญ์ state๋ฅผ ๋ง๋ค๊ณ , ๊ฐ๊ฐ state๋ฅผ ๋ด๋ ค๋ฐ์์ ํ์ฉ.

Image:ย https://user.oc-static.com/upload/2021/05/06/16202866148021_P3C3-2.png -
props ๋ฅผ ํตํด์ ๊ณ์ ์ ๋ฌํด์ค์ผ ํ๋ ๋จ์ ์กด์ฌ (props drilling)
-
-
Props drilling ๋ฌธ์ ํด๊ฒฐ: Context API ์ฌ์ฉ
-
์ผ์ชฝ์ Props drilling, ์ค๋ฅธ์ชฝ์ย Context API ์ฌ์ฉ
-
Provider ์ Consumer ๊ฐ๋ ์ผ๋ก ์ ๊ณต/์๋น ํ๋ ๊ฐ๋
-
props ๋ฅผ ๊ณ์ ์ ๋ฌํด์ค ํ์๊ฐ ์์.
-
React ๊ฐ ์ปดํฌ๋ํธ ๊ณ์ธต์ ๋ถ์ํด์ props๋ก ์ ๋ฌ์ํด

image:ย https://miro.medium.com/max/2000/1*Ha2vNB0ILaYKPXk6oyTZSQ.png
์์.
https://milddev.com/global-state-management-with-react-usestate-usecontext-hooks-and-context-api
์ฐธ๊ณ . React-Redux ์์๋ Context API๋ฅผ ์ฌ์ฉํ ๋ฐฉ์
import { createStore} from 'redux';
import { Provider } from 'react-redux';
//Provider
const store = createStore(rootReducer)
ReactDOM.render(
<Provider store={store}>
<App />
</Provider>,
document.getElementById('root')
)
//Consumer
import React from 'react'
import { useSelector } from 'react-redux'
export const CounterComponent = () => {
const counter = useSelector((state) => state.counter)
return <div>{counter}</div>
}
image:ย https://css-tricks.com/wp-content/uploads/2016/03/redux-article-3-03.svg?w=640
Props drilling ๋ฌธ์ ํด๊ฒฐ: Component composition
Composition ?
https://reactjs.org/docs/composition-vs-inheritance.html
Ex) ์ผ๋ฐ์ ์ธ ์ฝ๋
export default function App() {
return (
<div className="App">
<FirstComponent content="Who needs me?" />
</div>
);
}
function FirstComponent({ content }) {
return (
<div>
<h3>I am the first component</h3>;
<SecondComponent content={content} />|
</div>
);
}
function SecondComponent({ content }) {
return (
<div>
<h3>I am the second component</h3>;
<ThirdComponent content={content} />
</div>
);
}
function ThirdComponent({ content }) {
return (
<div>
<h3>I am the third component</h3>;
<ComponentNeedingProps content={content} />
</div>
);
}
function ComponentNeedingProps({ content }) {
return <h3>{content}</h3>;
}
๊ฐ์
function FirstComponent({ children }) {
return (
<div>
<h3>I am the first component</h3>;
{ children }
</div>
);
}
function SecondComponent({ children }) {
return (
<div>
<h3>I am the second component</h3>;
{children}
</div>
);
}
function ThirdComponent({ children }) {
return (
<div>
<h3>I am the third component</h3>
{children}
</div>
);
}
function ComponentNeedingProps({ content }) {
return <h3>{content}</h3>
}
export default function App() {
const content = "Who needs me?";
return (
<div className="App">
<FirstComponent>
<SecondComponent>
<ThirdComponent>
<ComponentNeedingProps content={content} />
</ThirdComponent>
</SecondComponent>
</FirstComponent>
</div>
);
}