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.

2
  • Why Linker can't be accesed with refs? You should direct call constructor of React$Element never Commented Jul 8, 2016 at 9:52
  • @Yozi Yes, found that out too, which works very nicely. Commented Jul 8, 2016 at 9:54

2 Answers 2

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();
Sign up to request clarification or add additional context in comments.

3 Comments

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.
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.
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.
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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.