Forum

Which Lifecycle Event Do You Make Ajax Requests And Why?

This thread contains 1 reply, has 2 voices, and was last updated by darshan.soni007 5 years, 3 months ago.

  • Author Replies
    • #52274

      Which Lifecycle Event Do You Make Ajax Requests And Why?


    • #52275

      AJAX requests should go in the componentDidMount lifecycle event.

      There are a few reasons for this,

      – Fiber, the next implementation of React’s reconciliation algorithm, will have the capacity to start and stop rendering as required for performance gains. One of the trade-offs of this is that componentWillMount, the other lifecycle event where it might make sense to make an AJAX request, will be “non-deterministic”. What this means is that React may start requesting componentWillMount at various times whenever it feels like it needs to. This would obviously be a bad formula for AJAX requests.

      – You can’t support the AJAX request won’t fix before the component mounts. If it did, that would mean that you’d be trying to setState on an unmounted component, which not only won’t work. Doing AJAX in componentDidMount will ensure that there’s a component to update.


Viewing 1 reply thread

You must be to reply to this thread.Please or . Registration is 100% free.

GET FREE CONSULTATION

Call Us Now For Free Consultation97370 05566

Our experts listen to you patiently and suggest you the right course after conducting a personality profile test. Register your interest below to schedule personality profile test for you.

SUBSCRIBE TO OUR NEWSLETTER

Copyright 2016-2024 Smart Mentors. All Rights Reserved.

Login

FORGOT PASSWORD

Sign up now to Become An Instructor

    Register your Interest

      cf7captchaRegenerate Captcha