export default class DetailsPage extends React.PureComponent {
constructor() {
super();
this.state = {
data: '',
isModalOpen: false,
};
**//this.openModal = this.openModal.bind(this);**
}
openModal() {
console.log(this, "this in Handler");
this.setState({ isModalOpen: true });
}
closeModal() {
this.setState({ isModalOpen: false });
}
케이스 1 (없음 클릭 핸들러에 바인딩)값은 함수
render() {
return (
(<div className="left">
<button className="btn btn btn-primary" type="button"
onClick={this.openModal}>Edit</button>
</div>
);
)
}
}
케이스 2 (이 바인딩)
결합없이 반작용render() {
return (
(<div className="left">
<button className="btn btn btn-primary" type="button"
onClick={this.openModal.bind(this)}>Edit</button>
</div>
);
)
}
}
내 클릭 핸들러 openModal()
글자 : console.log(this)
사례 1의 경우 : "THIS"의 값이 반응에서 NULL
이됩니다.
사례 2 : "THIS"의 값이 구성 요소의 값으로 나타납니다 (완벽하게 정상입니다).
내 의심 :
이유는 무엇입니까 "이"첫 번째 경우에
NULL
나옵니다.에
super()
의 용도는 무엇입니까? 나는 그것에 대해 읽었고 우리가super()
을 제거하고 super() & super (소품)의 차이를 제거하면 어떻게되는지 알게되었습니다. 그러나super()
으로 전화를 걸어 배경에서 발생하는 기능을 알고 싶습니다. pros and cons 항상이 일을 할 수있다,주의해야로서class Foo extends React.Component { someMethod =() => { // "this" is bound automatically! console.log(this, "this in Handler"); this.setState({ isModalOpen: true }); } }
- :하지만 내 마음에 드는 아마 ES2015 화살표 함수 구문입니다 -
일반적으로 'bind'로 바인딩 된 프로토 타입 메소드에는 화살표보다 [more benefits] (https://stackoverflow.com/a/43601993/3731501)이 있습니다. – estus
화살표 기능은이 솔루션에서 중요한 부분을 차지하지만 실제 실험 기능 (https://github.com/tc39/proposal-class-fields)을 사용하고 있습니다. 또한 언급 된 성능 논쟁은 * 인라인 * 이벤트 핸들러, 즉 render 메소드에 정의 된 함수에 관한 것입니다. 너의 건축 시간에 다른 생성됩니다. –