1

I know you can call static class method like this, but how would you achieve the same if it was not class but a function component.

import ReactDOM from "react-dom";

import "./styles.css";

class App extends Component {
  static staticMethod() {
    alert( 'static method has been called.');
  }

  render(){
    return <h1>hi</h1>
  }
}

App.staticMethod()

What I would like to achieve is call testMethod from outside of react component.

import React from "react";
import ReactDOM from "react-dom";

function App (props) {
  const testMethod = () => {
    return props.test
  }
  return( <h1>hi</h1>)
}

//not workoing
//App.testMethod();
  • if i understand you correct when you pass it to function component it will be pass as props so you can use it like this `props.funcName()` – Joseph Sep 20 '19 at 14:43
  • Possible duplicate of [How to call a component function on other component, but from the other component ? React](https://stackoverflow.com/questions/45262987/how-to-call-a-component-function-on-other-component-but-from-the-other-componen) – norbitrial Sep 20 '19 at 14:44
  • Answer to your question can be found here https://stackoverflow.com/questions/43474967/call-es5-class-method-from-static-method – Milind Agrawal Sep 20 '19 at 14:45
  • You need to learn more about the _static_ keywords here's an URL to MDN https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes/static – t3__rry Sep 20 '19 at 14:46

0 Answers0