React setstate method's callback is not fired












0















I have a strange question about set state's callback function in React. Everything is working fine in React 15 but once I upgraded the react version to 16, the set state's callback function is not getting fired. I have tried almost everything but I could not clearly find out what makes this issue.



I have tried binding the function in the constructor, using an anonymous function inside the set state method but none of them actually worked.



Then I tried downgrading the react version back to 15 and ran the project and I was able to fire the callback method (tested by putting an alert box inside the callback function).



Here's the exact code that i'm using in my sample project:



This function is called on a button click



incrementCounter() {
this.setState({
currentCount: this.state.currentCount + 1
}, this.callbackfuncsajad.bind(this));
}


This is the set state's callback function



callbackfuncsajad = () => {
alert('in the callback');
}


This is my package.json to check if there's any version mismatch:




  • "react": "^15.6.2"

  • "react-dom": "^16.6.3"

  • "@types/react": "^15.0.35"

  • "@types/react-dom": "^15.5.8"

  • "typescript": "2.4.1"


As a side note:
I am using Visual Studio 2017 ASP.NET's default React.JS template for my project. The project comes by default with React 15 along with typescript.



Would be a big time help if anyone could figure this issue out.



Thanks.










share|improve this question


















  • 1





    can u try binding that function to this in constructor and just pass it as callback

    – klvenky
    Nov 23 '18 at 9:25











  • Please, provide stackoverflow.com/help/mcve . It's unknown how incrementCounter is used. this could be already wrong there.

    – estus
    Nov 23 '18 at 9:27











  • There is a mismatch between the react and react-dom's version. Please keep the version of both react and react-dom same. If you want to use the latest version of react then keep "react": "16.6.3" and "react-dom": "16.6.3"

    – Hardik Modha
    Nov 23 '18 at 9:30








  • 1





    @klvenky The callback is an arrow function so no need to bind.

    – Hardik Modha
    Nov 23 '18 at 9:34
















0















I have a strange question about set state's callback function in React. Everything is working fine in React 15 but once I upgraded the react version to 16, the set state's callback function is not getting fired. I have tried almost everything but I could not clearly find out what makes this issue.



I have tried binding the function in the constructor, using an anonymous function inside the set state method but none of them actually worked.



Then I tried downgrading the react version back to 15 and ran the project and I was able to fire the callback method (tested by putting an alert box inside the callback function).



Here's the exact code that i'm using in my sample project:



This function is called on a button click



incrementCounter() {
this.setState({
currentCount: this.state.currentCount + 1
}, this.callbackfuncsajad.bind(this));
}


This is the set state's callback function



callbackfuncsajad = () => {
alert('in the callback');
}


This is my package.json to check if there's any version mismatch:




  • "react": "^15.6.2"

  • "react-dom": "^16.6.3"

  • "@types/react": "^15.0.35"

  • "@types/react-dom": "^15.5.8"

  • "typescript": "2.4.1"


As a side note:
I am using Visual Studio 2017 ASP.NET's default React.JS template for my project. The project comes by default with React 15 along with typescript.



Would be a big time help if anyone could figure this issue out.



Thanks.










share|improve this question


















  • 1





    can u try binding that function to this in constructor and just pass it as callback

    – klvenky
    Nov 23 '18 at 9:25











  • Please, provide stackoverflow.com/help/mcve . It's unknown how incrementCounter is used. this could be already wrong there.

    – estus
    Nov 23 '18 at 9:27











  • There is a mismatch between the react and react-dom's version. Please keep the version of both react and react-dom same. If you want to use the latest version of react then keep "react": "16.6.3" and "react-dom": "16.6.3"

    – Hardik Modha
    Nov 23 '18 at 9:30








  • 1





    @klvenky The callback is an arrow function so no need to bind.

    – Hardik Modha
    Nov 23 '18 at 9:34














0












0








0








I have a strange question about set state's callback function in React. Everything is working fine in React 15 but once I upgraded the react version to 16, the set state's callback function is not getting fired. I have tried almost everything but I could not clearly find out what makes this issue.



I have tried binding the function in the constructor, using an anonymous function inside the set state method but none of them actually worked.



Then I tried downgrading the react version back to 15 and ran the project and I was able to fire the callback method (tested by putting an alert box inside the callback function).



Here's the exact code that i'm using in my sample project:



This function is called on a button click



incrementCounter() {
this.setState({
currentCount: this.state.currentCount + 1
}, this.callbackfuncsajad.bind(this));
}


This is the set state's callback function



callbackfuncsajad = () => {
alert('in the callback');
}


This is my package.json to check if there's any version mismatch:




  • "react": "^15.6.2"

  • "react-dom": "^16.6.3"

  • "@types/react": "^15.0.35"

  • "@types/react-dom": "^15.5.8"

  • "typescript": "2.4.1"


As a side note:
I am using Visual Studio 2017 ASP.NET's default React.JS template for my project. The project comes by default with React 15 along with typescript.



Would be a big time help if anyone could figure this issue out.



Thanks.










share|improve this question














I have a strange question about set state's callback function in React. Everything is working fine in React 15 but once I upgraded the react version to 16, the set state's callback function is not getting fired. I have tried almost everything but I could not clearly find out what makes this issue.



I have tried binding the function in the constructor, using an anonymous function inside the set state method but none of them actually worked.



Then I tried downgrading the react version back to 15 and ran the project and I was able to fire the callback method (tested by putting an alert box inside the callback function).



Here's the exact code that i'm using in my sample project:



This function is called on a button click



incrementCounter() {
this.setState({
currentCount: this.state.currentCount + 1
}, this.callbackfuncsajad.bind(this));
}


This is the set state's callback function



callbackfuncsajad = () => {
alert('in the callback');
}


This is my package.json to check if there's any version mismatch:




  • "react": "^15.6.2"

  • "react-dom": "^16.6.3"

  • "@types/react": "^15.0.35"

  • "@types/react-dom": "^15.5.8"

  • "typescript": "2.4.1"


As a side note:
I am using Visual Studio 2017 ASP.NET's default React.JS template for my project. The project comes by default with React 15 along with typescript.



Would be a big time help if anyone could figure this issue out.



Thanks.







javascript asp.net reactjs callback setstate






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 23 '18 at 9:22









Sajad JawardSajad Jaward

124




124








  • 1





    can u try binding that function to this in constructor and just pass it as callback

    – klvenky
    Nov 23 '18 at 9:25











  • Please, provide stackoverflow.com/help/mcve . It's unknown how incrementCounter is used. this could be already wrong there.

    – estus
    Nov 23 '18 at 9:27











  • There is a mismatch between the react and react-dom's version. Please keep the version of both react and react-dom same. If you want to use the latest version of react then keep "react": "16.6.3" and "react-dom": "16.6.3"

    – Hardik Modha
    Nov 23 '18 at 9:30








  • 1





    @klvenky The callback is an arrow function so no need to bind.

    – Hardik Modha
    Nov 23 '18 at 9:34














  • 1





    can u try binding that function to this in constructor and just pass it as callback

    – klvenky
    Nov 23 '18 at 9:25











  • Please, provide stackoverflow.com/help/mcve . It's unknown how incrementCounter is used. this could be already wrong there.

    – estus
    Nov 23 '18 at 9:27











  • There is a mismatch between the react and react-dom's version. Please keep the version of both react and react-dom same. If you want to use the latest version of react then keep "react": "16.6.3" and "react-dom": "16.6.3"

    – Hardik Modha
    Nov 23 '18 at 9:30








  • 1





    @klvenky The callback is an arrow function so no need to bind.

    – Hardik Modha
    Nov 23 '18 at 9:34








1




1





can u try binding that function to this in constructor and just pass it as callback

– klvenky
Nov 23 '18 at 9:25





can u try binding that function to this in constructor and just pass it as callback

– klvenky
Nov 23 '18 at 9:25













Please, provide stackoverflow.com/help/mcve . It's unknown how incrementCounter is used. this could be already wrong there.

– estus
Nov 23 '18 at 9:27





Please, provide stackoverflow.com/help/mcve . It's unknown how incrementCounter is used. this could be already wrong there.

– estus
Nov 23 '18 at 9:27













There is a mismatch between the react and react-dom's version. Please keep the version of both react and react-dom same. If you want to use the latest version of react then keep "react": "16.6.3" and "react-dom": "16.6.3"

– Hardik Modha
Nov 23 '18 at 9:30







There is a mismatch between the react and react-dom's version. Please keep the version of both react and react-dom same. If you want to use the latest version of react then keep "react": "16.6.3" and "react-dom": "16.6.3"

– Hardik Modha
Nov 23 '18 at 9:30






1




1





@klvenky The callback is an arrow function so no need to bind.

– Hardik Modha
Nov 23 '18 at 9:34





@klvenky The callback is an arrow function so no need to bind.

– Hardik Modha
Nov 23 '18 at 9:34












1 Answer
1






active

oldest

votes


















0














Hi I think its due to parent context, so try somthing like this.



incrementCounter() {
this.setState({
currentCount: this.state.currentCount + 1
}, () => { this.callbackfuncsajad() });


}



Hope it helps.
If you still face this problem kindly share code for proper deduction.






share|improve this answer
























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53443771%2freact-setstate-methods-callback-is-not-fired%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    Hi I think its due to parent context, so try somthing like this.



    incrementCounter() {
    this.setState({
    currentCount: this.state.currentCount + 1
    }, () => { this.callbackfuncsajad() });


    }



    Hope it helps.
    If you still face this problem kindly share code for proper deduction.






    share|improve this answer




























      0














      Hi I think its due to parent context, so try somthing like this.



      incrementCounter() {
      this.setState({
      currentCount: this.state.currentCount + 1
      }, () => { this.callbackfuncsajad() });


      }



      Hope it helps.
      If you still face this problem kindly share code for proper deduction.






      share|improve this answer


























        0












        0








        0







        Hi I think its due to parent context, so try somthing like this.



        incrementCounter() {
        this.setState({
        currentCount: this.state.currentCount + 1
        }, () => { this.callbackfuncsajad() });


        }



        Hope it helps.
        If you still face this problem kindly share code for proper deduction.






        share|improve this answer













        Hi I think its due to parent context, so try somthing like this.



        incrementCounter() {
        this.setState({
        currentCount: this.state.currentCount + 1
        }, () => { this.callbackfuncsajad() });


        }



        Hope it helps.
        If you still face this problem kindly share code for proper deduction.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 23 '18 at 10:42









        kshitijkshitij

        342211




        342211
































            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53443771%2freact-setstate-methods-callback-is-not-fired%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            這個網誌中的熱門文章

            Xamarin.form Move up view when keyboard appear

            Post-Redirect-Get with Spring WebFlux and Thymeleaf

            Anylogic : not able to use stopDelay()