0

I have a like a scenario like this below

class Dashboard extends React.Component{

    constructor(){
        super();
        this.state = {opens: false};
    }
    check(){
        read(); //there is some other code which call this read function after completion 
        function read(){
            this.callnow();
        }
    }

    callnow() {
        alert("called");
    }

 render(){
   return (
            <div>               
                    <Button label="Check" onClick={this.check.bind(this)}  />  
          </div>
        );
    }
 }

on button click check function is called which call read function (this function is call back function which called after some browser activity happens) inside read function I need to call another method of Dashboard component which is callnow.

I know this can resolve using like

var self = this;

then use self inside the function.

I need to know how to use bind in this case ? I want to perform this case using bind only (just for knowledge purpose)

Kushal Jain
  • 3,029
  • 5
  • 31
  • 48
  • See the linked question's answers for details. Either `read.call(this);` or make `read` an arrow function: `const read = () => { this.callNow(); }; read();` – T.J. Crowder Jul 29 '17 at 13:00
  • thanks, but I cannot use arrow in my class and I also edit the question please have a look – Kushal Jain Jul 29 '17 at 13:05
  • There's nothing in your question to suggest any reason you can't use an arrow function. In any case, the linked question's answers also cover `bind`. – T.J. Crowder Jul 29 '17 at 13:06

0 Answers0