I have problem with web3js 1.2.9 Events triggering works fine and data is alright but I cannot figure out how to use component state in this functions that are triggered by event. I cannot access state of that component where watch event is initialized.

             .on("connected",function (subscriptionId) {console.log("SubscriptionId  ",subscriptionId)})
             .on("data",function (event) {
                 console.log("Event ",event)
                 console.log("Current users")

The function has its own this binding, so you cannot access this.state on the parent. To solve this, you can either:

  1. Use an arrow function, since they don't provide their own this binding:
      .on("data", (event) => {
  2. Bind the function to the parent's this object:
    function handleData (event) {
      .on("data", handleData.bind(this));

You can read more about this in JavaScript on the MDN docs here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/this.

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.