Phillipp
Phillipp

Reputation: 1445

Call a function on a ReactJs component class object

I have the following ReactJs class:

var Linker = React.createClass({
  foo: function(){
    console.log('foo')
  },

  render: function(){
    return (
      <div></div>
    );
  }
});

Now I want to call the foo() function from the outside:

Linker.foo();

But it throws:

app.js:4654 Uncaught TypeError: Linker.foo is not a function

Can anyone tell me how to call the foo() method?

Reason: I have to use an older version of react-router where I need an old ES5 class with some mixins to transition to a different route. My Plan is to let the Linker class do the transition while my main class is ES6.

Upvotes: 1

Views: 2896

Answers (2)

Phillipp
Phillipp

Reputation: 1445

I found the answer here: https://stackoverflow.com/a/24848228/3877081

I had to render the Linker component and can access it through refs.

Upvotes: 0

Dan Prince
Dan Prince

Reputation: 29989

If you need access to the function without any context, then you can find it directly on the prototype property for Linker.

Linker.prototype.foo();

Upvotes: 1

Related Questions