React Router & componentDidMount
This one is short.
Get Sam Strong’s stories in your inbox
Join Medium for free to get updates from this writer.
So I added React Router to my app and discovered that whenever I switched routes the components were being recreated (both constructor and componentDidMount being called). I then spent a bit of time trying to find out if this was intended behaviour since I was using componentDidMount for an XHR request and other SPA’s I’d worked on (using other tech) didn’t reload components when routing. The internet being the lovely contradictory mess that it is refused to provide me with an answer. Bugs, HoC’s, “/” being magical. All those were discussed.
In the end I asked on Reactiflux and got an answer from one of the mods. This is expected behaviour.
So, if you find yourself wondering about this, here it is: components being recreated every time you route somewhere is how React Router works.




