-
Notifications
You must be signed in to change notification settings - Fork 7.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Explain why componentDidMount is a better place to make an ajax request #302
Comments
I believe that the component to render’s state is last sent to the update queue in You may be able to understand more from the source: https://github.com/facebook/react/blob/master/packages/react-reconciler/src/ReactFiberClassComponent.js |
If it's an async request, it won't be fulfilled by the time the component mounts anyway, regardless of where you fire it. This is because JS is single threaded, and the network request can't "come back" and be handled while we are still rendering. So the difference between firing it earlier and later is often negligible. You're right that it matters in some rare cases though and for those cases it might make sense to break the recommendation. But you should be extra cautious as state can update before mounting, and if your data depends on state, you might have to refetch in that case. In other words: when in doubt, do it in The specific recommendation to avoid side effects in the constructor and |
I'll mark this issue as a "good first issue" in case anyone wants to take Dan's note about async and interruptions and add it to the docs. Put another way, in the future (once the async API is stable and you're using it), it will be possible for lifecycle hooks like That's why it's important for side-effects (eg your network requests) to live in methods like |
It's important to not overload the docs with speculations about future behavior though. If explanation raises more questions than it answers I'd argue it would only make the docs more complex for the majority of readers. |
I agree. But I also agree with OP that it's confusing and limiting for us to say "don't do this" without offering some hint at our rationale. My elaboration above wasn't meant as a suggestion for what we should add to the docs, but rather to help whoever may work on adding the note to better understand why. |
Thanks for the quick replies! I agree with you of course. I only felt it was counter intuitive, and wanted to know the whys of the recommendation, that's all. Perhaps in the future, when the API is stable, we can come up with a concise explanation for this recommendation. |
Hello I'm a beginner to open source contributions. Have gone through the guidelines and was wondering if I should take up this issue? |
This issue is all yours, @vishalvrv9! 😄 I've added an "in-progress" label so that others will know not to start work on the issue. If you change your mind about the issue, no worries! Just let me know so that I can remove the label and free it up for someone else to claim. Cheers! |
@gacosta89 @gaearon maybe I have misunderstood The sole purpose of componentWillMount is for prefetching such as Ajax calls as long as the state is updated on |
This is correct, @michaelgichia. Early prefetching is the canonical use for It's best to stick with read-only (eg GET) requests though, for the reasons mentioned above regarding future async behavior. I like Dan's summary above: "When in doubt, do it in
It is actually okay to set state from within That being said, it's unlikely that a GET request sent from |
@bvaughn thank you for taking your time to write a comprehensive explanation. I hope others will find this thread too for a better understanding of React Lifecycle and Setstate. |
@vishalvrv9 hi vishal! are you still working on this issue? i don't see a |
Firstly, sorry for not keeping anyone posted. @kevinkiklee Noticed there was no |
I've marked it in-progress then. 😄 |
not clear
right? but why? if before render, render the data. if after render, rerender it. is it not ok? |
…actjs#302) * docs(cn): translate content/docs/reference-profiler.md into Chinese * update due to feedback * fix due to feedback
In the docs, in componentWillMount section section it says:
And in the componentDidMount section:
Which for me is confusing because I won't like to wait for the component to be mounted to dispatch an ajax call to fulfill the component data dependencies. I would like to do it as soon as possible, like in the constructor, not even in componentWillMount.
Clearly you may have a reason why you say this in the docs, I might be not seeing the bigger picture. So it will be nice if you can explain a bit more in detail or point me in the direction of where I can find the reason.
The text was updated successfully, but these errors were encountered: