2

I am trying to update a value from the smart contract in te GUI - therefore I am using React.

I have the following event in my Smart Contract:

 event valueChange(uint old_value, uint new_value);

Im my DAPP I am listening to this event by the following lines of code (in the componentWillMount Method):

      //Event Triggering
  var myEvent = helloworldcontract.valueChange();

  myEvent.watch(function(error, result){

    if (!error) {

            console.log("New Value: " + result.args.new_value);

            console.log("Old Value: " + result.args.old_value);

            this.setState({lastNames: String(helloworldcontract.getText.call())})
    }

At the last line i get the following error

App.js:88 Uncaught TypeError: Cannot read property 'setState' of undefined

Do you know why I am getting this error? When I am using this line outside the Watch function it works fine.

3

When you use a function as a callback, this no longer refers to the same scope.

What you can do is something like

var myEvent = helloworldcontract.valueChange();
var _this = this;

myEvent.watch(function(error, result){

     if (!error) {

        console.log("New Value: " + result.args.new_value);

        console.log("Old Value: " + result.args.old_value);

        _this.setState({lastNames: String(helloworldcontract.getText.call())})
    }
}
  • Thx! that works now! – Wi1616 Jan 10 '17 at 18:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.