componentWillMount is executed before the INITIAL render component and is used to evaluate props and execute any additional logic based on them (as a rule, to update the state), and as such can be performed on the server in order to get the first markup on the server side.
componentDidMount runs AFTER the initial render when the DOM is updated (but above all, the ADDITIONAL DOM update is colored in the browser, which allows you to perform all kinds of advanced interactions with the DOM itself). This, of course, can happen only in the browser itself and therefore does not occur as part of the SSR, since the server can generate markup, not the DOM itself, this is done after it is sent to the browser if the SSR is used
Advanced DOM interactions that you say? Whaaaat ?? ... Yep - at this point, because the DOM has been updated (but the user has not yet seen the update in the browser), you can intercept the actual painting on the screen using window.requestAnimationFrame , and then do something like measure the actual elements The DOMs that will be output to which you can make additional state changes are super useful, for example, animating to the height of an element with unknown contents of variable length (since now you can measure the contents and assign height to the animation) or to avoid flash scripts Enta during some state change.
Be very careful though, to protect state changes in any componentDid... , because otherwise an endless loop may occur, because changing the state will also lead to re-rendering, and therefore to another componentDid... and on and on.
alechill Jul 6 '17 at 11:09 2017-07-06 11:09
source share