Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Call react function from JQ function

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

like image 414
M. Frydl Avatar asked Feb 16 '17 13:02

M. Frydl


1 Answers

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.

like image 160
Rúben Azevedo Avatar answered Sep 18 '22 23:09

Rúben Azevedo