728x90
๐ props, onClick, handleClick ์์ธํ๊ฒ ์ดํดํ๊ธฐ
Button
์ปดํฌ๋ํธ๋ฅผ ๋ชจ๋ํ(module
)์ํฌ ๋ ์ด๋ฆ์ ๋ฐ๋ก ์ ํ์ง ๋ง๊ณprops
๋ฅผ ์ฌ์ฉํ์ฌ ์์์ ๊ฐ์ ์ค์ ํด์ ๋ฐ๊ฟ ์ ์๊ฒ ๋ ์ค์ ํ์. ๋ค์์Button
์ปดํฌ๋ํธ๋ฅผ ์ธ๋ถ์์ ์ฌ์ฉํ ๋props
๋กbutton
์ ์ด๋ฆ์ ๋ณ๊ฒฝํ๋ ๊ฐ๋จํ ๊ธฐ๋ฅ์ด๋ค.
// components/App.js
const App = () => {
return <Button text="IT IS BUTTON"></Button>;
};
// components/Button/index.js
const Button = (props) => {
return <button>{props.text}</button>;
};
![](https://images.velog.io/images/abcd8637/post/7976f8a9-ccdb-4b2e-826b-d1c18d134c26/%E1%84%89%E1%85%B3%E1%84%8F%E1%85%B3%E1%84%85%E1%85%B5%E1%86%AB%E1%84%89%E1%85%A3%E1%86%BA%202021-05-15%2015.56.56.png)
Button
์ปดํฌ๋ํธ์handleClick
๊ธฐ๋ฅ์ ๋ฌ์์ฃผ๊ณ ์ถ์ ๋๋ ๋ง์ฐฌ๊ฐ์ง๋กhandleClick
์ปดํฌ๋ํธ๋ฅผprops
๋ก ๋ด๋ ค์ฃผ๋ฉด ๋๋ค. ๋ค๋งprops
๋ก ๋ฐ๊ณ ๋์props.onClick()
์ฒ๋ผ ๊ดํธ(()
)๋ฅผ ์ฌ์ฉํด์ผ ํด๋ฆญํ์ ๋ ํจ์๋ฅผ ๋ฐ๋ก ์คํํ ์ ์๋ค. ๊ดํธ(()
)๋ฅผ ๋ฃ์ง ์์ผ๋ฉด ์ค๋ฅ๊ฐ ๋๋ค.
728x90
// src/components/Button/index.js
const Button = (props) => {
return (
<button
onClick={() => {
console.log(" i'm button. ");
props.onClick();
}}
>
{props.text}
</button>
);
};
// src/components/App.js
const App = () => {
const handleClick = () => {
console.log(" i'm handleClick!. ")
}
return <Button text="IT IS BUTTON" onClick={handleClick}></Button>;
};
![](https://images.velog.io/images/abcd8637/post/6677995d-d00f-4e3c-8f6a-7d0be4c56915/%E1%84%89%E1%85%B3%E1%84%8F%E1%85%B3%E1%84%85%E1%85%B5%E1%86%AB%E1%84%89%E1%85%A3%E1%86%BA%202021-05-15%2016.02.00.png)
handleClick
์ ๋ค๋ฅธ ํจ์์ ๋ฃ์ด๋๊ณ ๊ทธ ํจ์๋ฅผ ์ ์ฉํ๊ณ ์ถ๋ค๋ฉด ๋ค์๊ณผ ๊ฐ์ด ์์ฑํ๋ฉด ๋๋ค. ์ด๋์๋ ๊ดํธ(()
)๋ฅผ ๋ฃ์ด์ฃผ๋๊ฒ์ ์์ง ๋ง์!
// src/components/Button/index.js
const Button = (props) => {
return (
<button
onClick={() => {
console.log(" i'm button. ");
props.onClick();
}}
>
{props.text}
</button>
);
};
// src/components/App.js
const App = () => {
const handleClick = () => {
console.log(" i'm handleClick!. ")
}
const handleClick2 = () => {
handleClick();
}
return <Button text="IT IS BUTTON" onClick={handleClick2}></Button>;
};
![](https://images.velog.io/images/abcd8637/post/6677995d-d00f-4e3c-8f6a-7d0be4c56915/%E1%84%89%E1%85%B3%E1%84%8F%E1%85%B3%E1%84%85%E1%85%B5%E1%86%AB%E1%84%89%E1%85%A3%E1%86%BA%202021-05-15%2016.02.00.png)
๋ฐ์ํ
๋๊ธ