How to bind 'this' to functions outside react class which is a callback from other component?

后端 未结 2 627
臣服心动
臣服心动 2021-01-28 11:04

I have a React Component such as :

function callback(params){..
// I need to use this.setstate but this callback function is called 
// from other component. How         


        
2条回答
  •  独厮守ぢ
    2021-01-28 11:42

    I don't really understand the question. I don't know if this is what you mean, but if you want to save the 'this' temporary variable, then just create a global array or single variable to store the 'this'.

    var thisTemp;
    
    function callback(params){..
    // use variable here
    thisTemp.blah();
    ...
    }
    
    class RespProperties extends Component { ..
    //Assign this to thisTemp
    thisTemp = this;
    ...
    }
    

提交回复
热议问题