React parent component에서 child component에 method 호출하는 방법


  • 꼭 아래 script를 html script로 추가해야 작동합니다.
    • https://cdnjs.cloudflare.com/ajax/libs/react/15.3.2/react-dom.js
    • https://cdnjs.cloudflare.com/ajax/libs/react/15.3.2/react-with-addons.js

class Child extends React.Component {
  componentDidMount() {
    this.props.onRef(this)
  }
  componentWillUnmount() {
    this.props.onRef(null)
  }
  method() {
    alert('do stuff')
  }
  render() {
    return 

Hello World!

} } // Any higher-order component (HOC), for examle: // withStyles https://github.com/kriasoft/isomorphic-style-loader // connect https://github.com/reactjs/react-redux // etc. class EnhancedChild extends React.Component { render() { return } } class Parent extends React.Component { onClick = () => { this.child.method() // do stuff }; render() { return (
(this.child = ref)} />
); } } ReactDOM.render(, document.getElementById('root'))



 




댓글

이 블로그의 인기 게시물

야구 WBC 한국 대표팀 외국계 선수 출전 가능한 이유 (혈통 규정 완벽 정리)

대기업 프론트엔드(frontend) 프레임워크 성향

입체각이란?