bier hier bier hier -4 years ago 139
Javascript Question

In reactjs how can I call a method from another component?

Just trying reactjs and came across the situation that I want to call a method on another component:

class MyComp extends React.Component {

callMe(){
...
}
}


So mycomp2:

import MyComp from 'myComp';

class MyComp2 extends React.Component {

test(){
MyComp.callMe();
}
}


How can I do this ?

Answer Source

If the method callMe doesn't use this, you can declear it as static to use it like that.

class MyComp extends React.Component {

    static callMe(){
    ...
    }
}

If not, you can use ref to make it work.

You may need: https://facebook.github.io/react/docs/refs-and-the-dom.html

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download