React Child에서 Parent의 함수로 Event Handler 등록하기

the Other Object·2023년 2월 23일
0

React에서, event 를 JSX로 연결 할 수 있다는 것은 → 어떤 component 에서 event에 따른 동작을 해당 component 이외에, 부모 component(자신을 사용하는 component) 에서 컨트롤 할 수 있도록 제공 가능하다는 뜻이다.
즉, props 나 state 를 이용해서 event handler 를 연결 할 수 있다는 이야기이다.


class TestChild extends React.Component {
  constructor (props) {
    super (props);
    this.ownHandler = this.ownHandler.bind(this);
  }
  
  ownHandler () {
  }
  
  render () {
    // 부모에서 props로 전달한 parenPassedHandler에
    // 부모가 자신(this)을 bind 했으면, 해당 함수가 called 되었을 떄,
    // 해당 함수의 this는 부모임.
  
  	const parentPassHandler = this.props.parentPassedHandler;
    
    return (
    	<button
      		onClick={this.ownHandler}
  		></button>
    )
  }

}



class TestParent extends React.Component {
	parentHandler () {
    }
  
  	render () {
    	const parentPassedHandler = this.parentHandler
    }
}

0개의 댓글