如何在子组件中访问父组件方法,如下例所示

时间:2017-11-23 05:23:46

标签: reactjs react-native

[[1, 2, 3, 4], [1, 2, 3], [1, 2]]

//我在ibos.js中只编写了一次axios方法,以及如何继承每个组件以及如何通过ibos.get()直接获得响应?............... .................................................. .................................................. .................................................. .................................................. .................................................. ...........................................

1 个答案:

答案 0 :(得分:0)



class First extends React.Component {
  first() {
    alert("First")
  }
  render() {
    return <div>sdfsdf</div>
  }
}

class Second extends First {
  second() {
    alert('Second')
  }
  render() {
    return <div>dsfsdfsdf</div>
  }
}

class Third extends Second {
  onButtonClick() {
    super.second()
    super.first()
  }
  render() {
    return <button onClick={() =>     {this.onButtonClick()}}>Hello</button>;
  }
}

ReactDOM.render(
  <Third />,
  document.getElementById('container')
);
&#13;
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script>
    <div id="container"></div>
&#13;
&#13;
&#13;

You should extend 'ibios' class instead of Chart Component in your child component and use keyword 'super' to use parent methods.  Otherwise you can send it as props and use it

    class BarChartComponent extends Ibios{
      constructor(props){
            super(props)
            this.state={};  
        }
       componentDidMount(){
          //example
           var url="http://localhost:9090",apiUrl="/reports",prametrs={xyz:ramki};
         var retriveResponse=super.get(url,apiUrl,prametrs)
         console.log(retriveResponse);
       }
      render(){
         return(<p>hi......</p>);
      }
    }