Do not make asynchronous calls within ReactJS's componentWillMount method

Reading Time: 1 minutes

It's a really bad idea to execute asynchronous calls, i.e. AJAX, within the componentWillMount() method. Well, on the ReactJS site, they simply say not to do, but do not mention why. I know now!
I experienced an infinite loop of componentWillMount while making an AJAX call inside componentWillMount. Shifting the code from componentWillMount to componentDidMount resolved my problem instantly; but to get there I lost about three hours of my life.

UPDATE: This was not the real problem. See more here!

Facebooktwittergoogle_plusredditpinterestlinkedin

2 comments

  1. Hi Oliver,

    Sorry for your lost. However could you elaborate more on how you get into the infinite loop?

    I'm using an async function in my componentWillMount function. Everything works fine except there'll be a small delay when loading starts.

    Lucas

Leave a Reply

Your email address will not be published. Required fields are marked *