1๏ธโฃ ๊ธฐ๋ณธ props ์ ๋ฌ
๊ฐ์ฅ ๊ธฐ๋ณธ์ ์ธ ๋ฐฉ๋ฒ์ผ๋ก, ๋ถ๋ชจ ์ปดํฌ๋ํธ์์ ์์ ์ปดํฌ๋ํธ๋ก ์ง์ props๋ฅผ ์ ๋ฌํฉ๋๋ค.
// ๋ถ๋ชจ ์ปดํฌ๋ํธ
function Parent() {
// 'color'๋ผ๋ ์ด๋ฆ์ props๋ฅผ Child ์ปดํฌ๋ํธ์ ์ ๋ฌํฉ๋๋ค.โจ
return <Child color="blue" />;
}
// ์์ ์ปดํฌ๋ํธ
function Child(props) {
// props ๊ฐ์ฒด๋ฅผ ํตํด ์ ๋ฌ๋ฐ์ 'color' ๊ฐ์ ์ฌ์ฉํ์ฌ ์คํ์ผ์ ์ ์ฉํฉ๋๋ค.
// 'props.color'๋ฅผ ํตํด ์ ๊ทผํ์ฌ 'color' ๊ฐ์ ๊ฐ์ ธ์ต๋๋ค.โจ
return <div style={{ color: props.color }}>This text is {props.color}!</div>;
}
export default Parent;
2๏ธโฃ ๊ตฌ์กฐ ๋ถํด ํ ๋น(Destructuring)์ ์ฌ์ฉํ props ์ถ์ถ
๊ตฌ์กฐ ๋ถํด ํ ๋น(Destructuring)์ ์ฌ์ฉํ๋ฉด, props์์ ํน์ ๊ฐ๋ง ์ถ์ถํ์ฌ ์ฌ์ฉํ ์ ์์ด ์ฝ๋๊ฐ ๋ ๊ฐ๊ฒฐํด์ง๋๋ค.
๏ผ ๊ตฌ์กฐ ๋ถํด ํ ๋น : ๊ฐ์ฒด๋ ๋ฐฐ์ด์ ํน์ ์์ฑ์ด๋ ์์๋ฅผ ์ฝ๊ฒ ์ถ์ถํ์ฌ, ๋ณ์์ ์ง์ ํ ๋นํ ์ ์๋๋ก ํด์ฃผ๋ JavaScript ๋ฌธ๋ฒ์ ๋๋ค. ์ด๋ฅผ ํตํด ์ฝ๋๊ฐ ๋ ๊ฐ๊ฒฐํ๊ณ ๊ฐ๋ ์ฑ์ด ์ข์์ง๋๋ค.
// ๋ถ๋ชจ ์ปดํฌ๋ํธ
function Parent() {
// 'color'๋ผ๋ ์ด๋ฆ์ props๋ฅผ Child ์ปดํฌ๋ํธ์ ์ ๋ฌํฉ๋๋ค.
return <Child color="blue" />;
}
// ์์ ์ปดํฌ๋ํธ
function Child({ color }) {
// ๊ตฌ์กฐ ๋ถํด ํ ๋น์ ์ฌ์ฉํ์ฌ props ๊ฐ์ฒด์์ 'color'๋ผ๋ ๊ฐ์ ์ง์ ๋ฐ์์ต๋๋ค.
// ์ด๋ฅผ ํตํด 'props.color' ๋์ ๋ฐ๋ก 'color'๋ก ์ ๊ทผํ ์ ์์ด ์ฝ๋๊ฐ ๊ฐ๊ฒฐํด์ง๋๋ค.โจ
return <div style={{ color }}>This text is {color}!</div>;
}
export default Parent;
3๏ธโฃ ์คํ๋ ๋(Spread) ์ฐ์ฐ์๋ฅผ ์ฌ์ฉํ props ์ ๋ฌ
๋ถ๋ชจ ์ปดํฌ๋ํธ์์ ์ฌ๋ฌ ๊ฐ์ props๋ฅผ ์ ๋ฌํ ๋, ์คํ๋ ๋ ์ฐ์ฐ์(...)๋ฅผ ์ฌ์ฉํ์ฌ ํ ๋ฒ์ ์ ๋ฌํ ์ ์์ต๋๋ค.
๏ผ ์คํ๋ ๋ ์ฐ์ฐ์ : ๋ฐฐ์ด์ด๋ ๊ฐ์ฒด์ ๋ชจ๋ ์์๋ฅผ ๊ฐ๋ณ์ ์ผ๋ก ๋ถํดํ์ฌ ์ฌ์ฉํ ์ ์๊ฒ ํด์ฃผ๋ JavaScript ๋ฌธ๋ฒ์ ๋๋ค. ์ด๋ฅผ ํตํด ๋ฐฐ์ด์ด๋ ๊ฐ์ฒด๋ฅผ ๋ณต์ฌํ๊ฑฐ๋ ํฉ์น ๋ ์ ์ฉํ๊ฒ ์ฌ์ฉํ ์ ์์ต๋๋ค.
// ๋ถ๋ชจ ์ปดํฌ๋ํธ
function Parent() {
const props = {
message: "Hello from Parent",
color: "blue",
fontSize: "16px",
};
// ์คํ๋ ๋ ์ฐ์ฐ์๋ฅผ ์ฌ์ฉํ์ฌ ๋ชจ๋ props๋ฅผ ํ ๋ฒ์ ์์ ์ปดํฌ๋ํธ์ ์ ๋ฌํฉ๋๋ค.โจ
return <Child {...props} />;
}
// ์์ ์ปดํฌ๋ํธ
function Child({ message, color, fontSize }) {
// ๊ตฌ์กฐ ๋ถํด ํ ๋น์ ์ฌ์ฉํ์ฌ ์ ๋ฌ๋ฐ์ props์์ 'message', 'color', 'fontSize'๋ฅผ ์ถ์ถํฉ๋๋ค.โจ
// ์ด๋ฅผ ํตํด ๊ฐ ๊ฐ์ ์ฝ๊ฒ ์ฌ์ฉํ ์ ์์ต๋๋ค.
return <div style={{ color, fontSize }}>{message}</div>;
}
export default Parent;
4๏ธโฃ ๊ธฐ๋ณธ๊ฐ ์ค์ (Default Props)
์ปดํฌ๋ํธ์์ props๊ฐ ์ ๋ฌ๋์ง ์์์ ๊ฒฝ์ฐ ๊ธฐ๋ณธ๊ฐ์ ์ค์ ํ๋ ๋ฐฉ๋ฒ์ ํจ์ ๋งค๊ฐ๋ณ์ ๊ธฐ๋ณธ๊ฐ์ ์ฌ์ฉํ๋ ๋ฐฉ๋ฒ๊ณผ `defaultProps`๋ฅผ ์ฌ์ฉํ๋ ๋ฐฉ๋ฒ์ด ์์ต๋๋ค.
// ์์ ์ปดํฌ๋ํธ
function Child({ message = "Default Message" }) {
// 'message'๊ฐ ์ ๋ฌ๋์ง ์์์ ๊ฒฝ์ฐ ๊ธฐ๋ณธ๊ฐ์ ์ฌ์ฉํฉ๋๋ค.โจ
return <div>{message}</div>;
}
// ๋๋ defaultProps๋ฅผ ์ฌ์ฉํด ๊ธฐ๋ณธ๊ฐ์ ์ค์ ํ ์ ์์ต๋๋ค.โจ
Child.defaultProps = {
message: "Default Message",
};
export default Child;
5๏ธโฃ ํ์ ์ง์ props (PropTypes ์ฌ์ฉ)
์ปดํฌ๋ํธ๊ฐ ๋ฐ๋ props์ ์ ํ์ ์ง์ ํ ์๋ ์์ต๋๋ค. ์ด๋ ์ฃผ๋ก ๊ฐ๋ฐ ์ค์ ์ ์ฉํ๋ฉฐ, PropTypes๋ฅผ ์ฌ์ฉํ์ฌ ์ค์ ํฉ๋๋ค.
import PropTypes from 'prop-types';
// ์์ ์ปดํฌ๋ํธ
function Child({ message }) {
// 'message'๋ฅผ ํ๋ฉด์ ์ถ๋ ฅํฉ๋๋ค.
return <div>{message}</div>;
}
// 'message' props์ ํ์
์ ๋ฌธ์์ด๋ก ์ง์ ํ๊ณ ํ์๋ก ์๊ตฌํฉ๋๋ค.โจ
Child.propTypes = {
message: PropTypes.string.isRequired,
};
export default Child;
6๏ธโฃ ์ฝ๋ฐฑ ํจ์๋ฅผ ํตํ props ์ ๋ฌ
๋ถ๋ชจ ์ปดํฌ๋ํธ์์ ์์ ์ปดํฌ๋ํธ๋ก ๋ฐ์ดํฐ๋ฅผ ์ ๋ฌํ ๋ฟ๋ง ์๋๋ผ, ์์ ์ปดํฌ๋ํธ์์ ๋ถ๋ชจ ์ปดํฌ๋ํธ๋ก ์ด๋ฒคํธ๋ ๋ฐ์ดํฐ๋ฅผ ์ ๋ฌํ ๋๋ ์ฝ๋ฐฑ ํจ์๋ฅผ ์ฌ์ฉํฉ๋๋ค.
// ๋ถ๋ชจ ์ปดํฌ๋ํธ
function Parent() {
// ์์ ์ปดํฌ๋ํธ์์ ํธ์ถํ ํจ์๋ฅผ ์ ์ํฉ๋๋ค.
const handleChildClick = (data) => {
console.log("Data from child:", data);
};
// ์์ ์ปดํฌ๋ํธ์ 'onClick' ์ฝ๋ฐฑ ํจ์๋ฅผ ์ ๋ฌํฉ๋๋ค.โจ
return <Child onClick={handleChildClick} />;
}
// ์์ ์ปดํฌ๋ํธ
function Child({ onClick }) {
// ๋ฒํผ ํด๋ฆญ ์ ๋ถ๋ชจ๋ก๋ถํฐ ์ ๋ฌ๋ฐ์ ์ฝ๋ฐฑ ํจ์๋ฅผ ํธ์ถํฉ๋๋ค.
return <button onClick={() => onClick("Hello from Child")}>Click Me</button>;
}
export default Parent;
7๏ธโฃ ์กฐ๊ฑด๋ถ props ์ ๋ฌ
ํน์ ์กฐ๊ฑด์ ๋ฐ๋ผ props๋ฅผ ์ ํ์ ์ผ๋ก ์ ๋ฌํ ์ ์์ต๋๋ค.
// ๋ถ๋ชจ ์ปดํฌ๋ํธ
function Parent({ show }) {
// 'show'๊ฐ true์ผ ๋๋ง 'message' props๋ฅผ ์ ๋ฌํฉ๋๋ค.โจ
return <Child message={show ? "Visible" : undefined} />;
}
// ์์ ์ปดํฌ๋ํธ
function Child({ message }) {
// 'message'๊ฐ ์์ ๊ฒฝ์ฐ ๊ธฐ๋ณธ ๋ฉ์์ง๋ฅผ ํ์ํฉ๋๋ค.
return <div>{message ? message : "No message"}</div>;
}
export default Parent;
8๏ธโฃ Context๋ฅผ ์ฌ์ฉํ ์ ์ญ props ์ ๋ฌ
๋ฆฌ์กํธ์ Context API๋ฅผ ์ฌ์ฉํ๋ฉด ์ปดํฌ๋ํธ ํธ๋ฆฌ์ ๊น์ ๊ณณ๊น์ง props๋ฅผ ์ง์ ์ ๋ฌํ์ง ์๊ณ ๋ ๊ฐ์ ๊ณต์ ํ ์ ์์ต๋๋ค. ์ด๋ ์ ์ญ ์ํ ๊ด๋ฆฌ์ ์ ์ฉํฉ๋๋ค.
import React, { createContext, useContext } from 'react';
// Context๋ฅผ ์์ฑํฉ๋๋ค.โจ
const MyContext = createContext();
// ๋ถ๋ชจ ์ปดํฌ๋ํธ
function Parent() {
const value = "Hello from Context";
// Context Provider๋ฅผ ์ฌ์ฉํ์ฌ ์์ ์ปดํฌ๋ํธ์ ๊ฐ์ ์ ๋ฌํฉ๋๋ค.โจ
return (
<MyContext.Provider value={value}>
<Child />
</MyContext.Provider>
);
}
// ์์ ์ปดํฌ๋ํธ
function Child() {
// useContext ํ
์ ์ฌ์ฉํ์ฌ Context ๊ฐ์ ์ฌ์ฉํฉ๋๋ค.โจ
const message = useContext(MyContext);
return <div>{message}</div>;
}
export default Parent;
9๏ธโฃ children์ ํตํ props ์ ๋ฌ
React ์ปดํฌ๋ํธ๋ ํน๋ณํ props์ธ children์ ํตํด ์ปดํฌ๋ํธ ๋ด๋ถ์ HTML ์์๋ ๋ค๋ฅธ ์ปดํฌ๋ํธ๋ฅผ ์ฝ์ ํ ์ ์์ต๋๋ค. ์ด๋ฅผ ํตํด ๋ถ๋ชจ ์ปดํฌ๋ํธ์์ ์์ ์ปดํฌ๋ํธ๋ก ์ ์ฐํ๊ฒ ์ฝํ ์ธ ๋ฅผ ์ ๋ฌํ ์ ์์ผ๋ฉฐ, ๊ธฐ๋ณธ ๋ ์ด์์์ ์ก์ ๋ ๋งค์ฐ ์ ์ฉํฉ๋๋ค.
// ๋ถ๋ชจ ์ปดํฌ๋ํธ
function Parent() {
// 'color'๋ผ๋ ์ด๋ฆ์ props๋ฅผ ์ ๋ฌํ๊ณ , 'children'์ ํตํด ์์ ์ปดํฌ๋ํธ์ ์ฝํ
์ธ ๋ฅผ ์ ๋ฌํฉ๋๋ค.
// 'children'์ ๋ถ๋ชจ ์ปดํฌ๋ํธ์์ ์์ ์ปดํฌ๋ํธ ํ๊ทธ ์ฌ์ด์ ์์ฑ๋ ์์๋ค์ ์๋ฏธํฉ๋๋ค.
// ์ฌ๊ธฐ์๋ <h1>Hello, World!</h1>๊ฐ 'children'์ผ๋ก ์ ๋ฌ๋ฉ๋๋ค.โจ
return (
<Child color="blue">
{/* ์ด ์ฝํ
์ธ ๊ฐ ์์ ์ปดํฌ๋ํธ์ 'children' props๋ก ์ ๋ฌ๋ฉ๋๋ค. */}
<h1>Hello, World!</h1>
</Child>
);
}
// ์์ ์ปดํฌ๋ํธ
function Child({ color, children }) {
// 'color' props์ 'children'์ ์ฌ์ฉํ์ฌ ์ฝํ
์ธ ๋ฅผ ๋ ๋๋งํฉ๋๋ค.โจ
return (
<div style={{ color }}>
{children}
</div>
);
}
export default Parent;
๐ Render Props ํจํด์ ์ฌ์ฉํ props ์ ๋ฌ
Render Props ํจํด์ ์ฌ์ฉํ๋ฉด ํจ์ํ์ผ๋ก ์์ ์ปดํฌ๋ํธ๋ฅผ ์ ๋ฌํ์ฌ ์์ ์ปดํฌ๋ํธ๊ฐ ๋ถ๋ชจ ์ปดํฌ๋ํธ์ ๋ฐ์ดํฐ๋ฅผ ๋ ๋๋งํ ์ ์์ต๋๋ค.
// ๋ถ๋ชจ ์ปดํฌ๋ํธ
function Parent({ render }) {
const data = "Hello from Parent";
// 'render' ํจ์๋ฅผ ์ฌ์ฉํ์ฌ ์์ ์ปดํฌ๋ํธ์ ๋ฐ์ดํฐ๋ฅผ ์ ๋ฌํฉ๋๋ค.โจ
return <div>{render(data)}</div>;
}
// ์ฌ์ฉ ์์
function App() {
return (
<Parent render={(data) => <Child message={data} />} />
);
}
// ์์ ์ปดํฌ๋ํธ
function Child({ message }) {
return <div>{message}</div>;
}
export default App;