쪼렙 as! 풀스택

React - 자식 Component 객체에 접근하기! 본문

개발 일지/Web & Server

React - 자식 Component 객체에 접근하기!

코코앱 2018. 8. 22. 15:51


import { Component } from 'react';

class Child extends Component {
constructor(props) {
super(props)
if(props.onParentRef) {
props.onParentRef(this);
}
}

blahblah = () => {
console.log("BlahBlah~");
}

render() {
return (
<div>
<h2>여기는 Child 컴포넌트. </h2>
</div>
);
}
}


class Parent extends Component {
constructor(props) {
super(props)
this.childComponent;
}

callChildMethod = () => {
this.childComponent.blahblah()
}

render() {
return (
<div>
<h1>여기는 부모 컴포넌트.</h1>
<Child onParentRef={ref => (this.childComponent = ref)} />
<button onClick={this.callChildMethod}>블라블라</button>
</div>
);
}
}


export default Parent;





Comments