admin管理员组

文章数量:1400455

I need to call React function (this.clearMath()) from JQ function

$('.input-content').focus(
    function(){
        this.clearMath()
    })

I got Uncaught TypeError: this.clearMath is not a function. I think it´s caused by JQ that thinks this. is reference to selected element $('.input-content').

Am I right? And how to distinguish between react this an jquery this to be able call my function? Thanks

I need to call React function (this.clearMath()) from JQ function

$('.input-content').focus(
    function(){
        this.clearMath()
    })

I got Uncaught TypeError: this.clearMath is not a function. I think it´s caused by JQ that thinks this. is reference to selected element $('.input-content').

Am I right? And how to distinguish between react this an jquery this to be able call my function? Thanks

Share Improve this question asked Feb 16, 2017 at 13:48 M. FrydlM. Frydl 731 silver badge9 bronze badges 2
  • this makes reference to the scope where you currently are. In this case, you are right, the scope is of the selector. I'm not sure why are you trying to call a React function from jQuery. That doesn't seems right. As far as I'm concerned, it is not possible to call a React function defined inside a React ponent from jQuery. – juanecabellob Commented Feb 16, 2017 at 13:56
  • Can you post your code in a jsfiddle for better understanding what you are trying to do? – juanecabellob Commented Feb 16, 2017 at 13:59
Add a ment  | 

1 Answer 1

Reset to default 8

You can solve this by doing this:

var _ = this;
$('.input-content').focus(
function(){
    //this is still the input
    _.clearMath()
})

So you save the this context before the selector so you can access the _ inside the function, its called a closure.

本文标签: javascriptCall react function from JQ functionStack Overflow