1

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.

Phillipp
  • 1,425
  • 2
  • 12
  • 27
  • Why Linker can't be accesed with [refs](https://facebook.github.io/react/docs/more-about-refs.html)? You should direct call constructor of React$Element never – Yozi Jul 08 '16 at 09:52
  • @Yozi Yes, found that out too, which works very nicely. – Phillipp Jul 08 '16 at 09:54

2 Answers2

1

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();
Dan Prince
  • 29,491
  • 13
  • 89
  • 120
  • That didn't work in my case because without context, he can't find the router. But it seems to work when you don't need the context. – Phillipp Jul 08 '16 at 09:56
  • No worries. You might want to consider editing and re-wording the question before marking the accepted answer, as in the example you were looking for the method on the factory instance, not the component instance. – Dan Prince Jul 08 '16 at 09:59
  • I edited the question and marked your answer as the correct one because it is indeed the right answer of my question, even if it doesn't work for me because I need the context. – Phillipp Jul 08 '16 at 10:02
0

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

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

Community
  • 1
  • 1
Phillipp
  • 1,425
  • 2
  • 12
  • 27