티스토리 뷰
반응형
1. this 바인딩과 함께 전달
class MyComponent extends React.Component {
myMethod(param, e) {
console.log(param); // 'someParam'
console.log(e); // SyntheticEvent
}
render() {
return (
<button onClick={this.myMethod.bind(this, 'someParam')}></button> {/* this 바인딩과 함께 전달 */}
);
}
}
2. arrow function 활용
class MyComponent extends React.Component {
myMethod(param, e) {
console.log(param); // 'someParam'
console.log(e); // undefined
}
render() {
return (
<button onClick={() => {this.myMethod('someParam')}></button> {/* arrow function 활용 */}
);
}
}
React 합성 이벤트가 필요한 경우 명시해주어야 한다.
class MyComponent extends React.Component {
myMethod(param, e) {
console.log(param); // 'someParam'
console.log(e); // SyntheticEvent
}
render() {
return (
<button onClick={(e) => {this.myMethod('someParam', e)}></button> {/* arrow function 활용 */}
);
}
}
반응형
'프론트엔드' 카테고리의 다른 글
[typescript] enum과 const object 사용 문제 및 보완 (0) | 2021.07.27 |
---|---|
안드로이드 기기에서 실행중인 크롬, PC 크롬으로 디버깅하기 (0) | 2021.01.16 |
[React] 이벤트 핸들러에서 this 사용하기 (0) | 2020.10.27 |
iPhone Webview에서 Javascript 이벤트 발생 위치가 불일치하는 현상 (0) | 2020.09.15 |
Mobile Safari에서 bounce scroll 방지 (0) | 2020.09.03 |
댓글
반응형
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total