๐ React ์ ๋ฌธ โ จ - Event
๐ React ์
๋ฌธ โ
จ - Event
๐
ใ์ํ์ ์ฒ์ ๋ง๋ ๋ฆฌ์กํธใ
๋ฅผ ์ฝ๊ณ ์ ๋ฆฌํ ๊ธ์ ๋๋ค.
Event
์ฒ๋ฆฌ
1
2
3
4
5
// DOM
<button onclick="activate()">Activate</button>
// ๋ฆฌ์กํธ
<button onClick={activate}>Activate</button>
DOM
์๋React
์๋Event
๊ฐ ์์ง๋ง ์ฌ์ฉํ๋ ๋ฐฉ๋ฒ์ด ์กฐ๊ธ ๋ค๋ฅด๋ค.Event
๊ฐ ๋ฐ์ํ์ ๋ ํด๋นEvent
๋ฅผ ์ฒ๋ฆฌํ๋ ํจ์๊ฐ ์๋๋ฐ ์ด๊ฒ์Event Handler
๋๋Event Listener
๋ผ๊ณ ํ๋ค.
โ
Class Component
์ Event
์ฒ๋ฆฌ
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
class Toggle extends React.Component {
constructor(props) {
super(props);
this.state = { isToggleOn : true };
this.handleClick = this.handleClick.bind(this);
}
handleClick() {
this.setState(prevState => ({
isToggleOn : !prevState.isToggleOn
}));
}
render() {
return (
<button onClick={this.handleClick}>
{this.state.isToggleOn ? '์ผ์ง' : '๊บผ์ง'}
</button>
);
}
}
Class Component
๋ย ๊ธฐ๋ณธ์ ์ผ๋กBinding
๋์ง ์๋๋ค.ย- ๊ทธ๋ ๊ธฐ ๋๋ฌธ์ ์ง์
Bind
ํ์ง ์์ผ๋ฉดยthis.handleClick
์Global Scope
์์ ํธ์ถ๋์ง๋ง,Global Scope
์์ ํด๋น ๊ฐ์ยundefined
์ด๋ฏ๋ก ์ฌ์ฉํ ์ ์๋ค. - ๋ฐ๋ผ์ ํจ์์ ์ด๋ฆ ๋ค์ ๊ดํธ ์์ด ์ฌ์ฉํ๋ ค๋ฉด ํด๋น ํจ์๋ฅผ
Bind
ํด์ฃผ์ด์ผ ํ๋ค. - ์๋๋ผ๋ฉด ๋ค์์ฒ๋ผ ํ์ดํ ํจ์๋ฅผ ์ฌ์ฉํด์ผ ํ๋ค.
1
2
3
4
5
6
7
8
9
10
11
12
13
class MyButton extends React.Component {
handleClick () {
console.log('this is:', this);
}
rendor() {
return (
<button onClick={() => this.handleClick()}>
ํด๋ฆญ
</button>
);
}
}
- ํ์ดํ ํจ์๋ ์์ ์ด ์ข
์๋
Instance
๋ฅผ ๊ฐ๋ฆฌํจ๋ค.ย
โ
Functional Component
์ Event
์ฒ๋ฆฌ
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
function Toggle(props) {
const [isToggleOn, setIsToggleOn] = useState(true);
// ํจ์ ์์ ํจ์
function handleClick() {
setIsToggleOn((isToggleOn) => !isToggleOn);
}
// ํ์ดํ ํจ์
/*
const handleClick = () => {
setIsToggleOn((isToggleOn) => !isToggleOn);
}
*/
return (
<button onClick={handleClick}>
{isToggleOn ? '์ผ์ง' : '๊บผ์ง'}
</button>
);
}
- ์๊ธฐ ์ฝ๋๋ ์์ย
Toggle
ยClass Component
๋ฅผFunctional Component
๋ก ๋ณํํ ๊ฒ์ด๋ฉฐ, 1๋ฒ ๋ฐฉ์๊ณผ 2๋ฒ ๋ฐฉ์ ๋ชจ๋ ์ฌ์ฉ ๊ฐ๋ฅํ๋ค.ย
Arguments
1
2
3
4
5
6
7
8
9
function myButton(props) {
const handleDelete = (id, event) => {
console.log(id, event.target);
}
return (
<button onClick={(event) => handleDelete(1, event)}>์ญ์ ํ๊ธฐ</button>
);
}
- ํ์ดํ ํจ์ ๋ฐฉ์์ผ๋ก
handleDelete()
๋ฉ์๋๋ฅผ ํธ์ถํ๋ฉฐid
์event
๋ฅผ ์ ๋ฌํ๊ณ ์๋ค.
์ค์ต
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
import { useState } from "react";
function ConfirmButton(props) {
const [isConfirmed, setIsConfirmed] = useState(false);
const handleConfirm = () => {
setIsConfirmed((prevIsConfirmed) => !prevIsConfirmed);
};
const buttonStyle = {
padding: "10px 20px",
backgroundColor: isConfirmed ? "#aaa" : "#007BFF",
color: "white",
border: "none",
borderRadius: "4px",
fontSize: "14px",
cursor: isConfirmed ? "not-allowed" : "pointer",
transition: "background-color 0.3s ease",
marginTop: "20px",
marginLeft: "20px",
};
return (
<button onClick={handleConfirm} disabled={isConfirmed} style={buttonStyle}>
{isConfirmed ? '์ธ์ฆ์๋ฃ' : '์ธ์ฆ'}
</button>
);
}
export default ConfirmButton;
ํ๊ณ
- ์ค์ต ์์ ๋ฅผ ์ค์ ๋ก ์คํํด๋ณด๋ฉฐ ์ด๋ฉ์ผ ์ธ์ฆ ๋ฒํผ ๋ฑ ์ ์ฉํ๊ฒ ์ฐ์ด๊ณ ์ถฉ๋ถํ ์ฌ์ฌ์ฉ๋ ๊ฐ๋ฅํ
Component
๋ผ๋ ์๊ฐ์ด ๋ค์๋ค.
This post is licensed under CC BY 4.0 by the author.