์ค๋์ React์ ๊ณ ๊ธ ๊ธฐ๋ฅ๋ค์ ๊น์ด ์๊ฒ ํ์ตํ์ต๋๋ค. ํนํ ํด๋ก์ ์ ํ์ฉ, ์ปดํฌ๋ํธ ์ค์ฒฉ, ํผ ์ ์ถ ์ต์ ํ, ๊ทธ๋ฆฌ๊ณ ํด๋์ค๋ค์ ๊ด๋ฆฌ ๋ฐฉ๋ฒ์ ๋ํด ์ง์ค์ ์ผ๋ก ๊ณต๋ถํ์ต๋๋ค.
function debounce(func, delay) {
let timer;
clearTimeout(timer); // ์ฆ์ ์คํ๋์ด ๋ฒ๋ฆผ
timer = setTimeout(() => func(), delay);
}
์ด ๋ฐฉ์์ ๋ฌธ์ ์ :
function debounce(func, delay) {
let timer; // ํด๋ก์ ๋ก ์ ์ง๋๋ ๋ณ์
return (...args) => {
clearTimeout(timer);
timer = setTimeout(() => func(...args), delay);
};
}
์ฅ์ :
const ParentComponent = ({ children }) => {
return (
<div>
<h1>Parent Title</h1>
<div>{children}</div>
</div>
);
};
// ์ฌ์ฉ ์์
<ParentComponent>
<ChildComponent />
</ParentComponent>
const ParentComponent = ({ children }) => {
// ์์ ์์ ์กฐ์
React.Children.forEach(children, child => {
console.log(child.type);
});
// ์กฐ๊ฑด๋ถ ๋ ๋๋ง
if (!Array.isArray(children)) {
return <div>Single child: {children}</div>;
}
return (
<div>
{children.map((child, index) => (
<div key={index}>{child}</div>
))}
</div>
);
};
<form className={styles.formGrid}>
<button
type="button"
onClick={(e) => addNationHandler(e)}
className={styles.formButton}
>
๊ตญ๊ฐ ์ถ๊ฐ
</button>
<button
type="button"
onClick={(e) => modifyNationHandler(e)}
className={styles.formButton}
>
์
๋ฐ์ดํธ
</button>
</form>
const handleSubmit = (e) => {
e.preventDefault();
const buttonName = e.nativeEvent.submitter.name;
if (buttonName === 'add') {
addNationHandler();
} else if (buttonName === 'modify') {
modifyNationHandler();
}
};
<form className={styles.formGrid} onSubmit={handleSubmit}>
<button type="submit" name="add" className={styles.formButton}>
๊ตญ๊ฐ ์ถ๊ฐ
</button>
<button type="submit" name="modify" className={styles.formButton}>
์
๋ฐ์ดํธ
</button>
</form>
const TableWrapper = ({ children, wrapperClassName, headerClassName }) => (
<table className={wrapperClassName}>
<thead className={headerClassName}>
{children}
</thead>
</table>
);
import classNames from 'classnames';
const TableWrapper = ({
children,
wrapperClassName,
headerClassName,
isActive
}) => {
const tableClass = classNames(wrapperClassName, {
'active-class': isActive,
'table-wrapper': true
});
return (
<table className={tableClass}>
<thead className={headerClassName}>
{children}
</thead>
</table>
);
};
ํด๋ก์ ์ฌ์ฉ ์
children ํ์ฉ ์
ํผ ์ ์ถ ์ฒ๋ฆฌ ์
ํด๋์ค๋ค์ ๊ด๋ฆฌ ์