I think the prevailing wisdom that listeners should set in componentDidMount because it prevents problems in isomorphic applications is a mistake. I think that in 98% of cases for non-isomorphic applications, setting listeners in componentWillMount and componentDidMount will work the same way, but this is conceptually wrong and in 2% of cases (for example, the example given in the original question), it will do the wrong thing.
There are git discussions and comments in the React source code suggesting that it would be preferable that componentWillMount is not called at all on the server, but if it is not, then problems are created in the checksum test comparing the prerender server with the original client rendering. Having a componentWillMount on the server means that it is not running as part of the component life cycle in this case, but it is used as an excuse not to consider it part of the life cycle in any case.
In fact, componentWillMount is exactly the place where you need to register listeners if you are not creating an isomorphic application. If you are creating an isomorphic application, you need to make some tradeoffs because the checksum / lifecycle problem is not ideal in this case (maybe this is just testing for a server environment and then not registering listeners?).
In non-isomorphic applications, adding listeners to componentWillMount in some cases can save unnecessary re-renderings and register them in document order. The advantage of ordering a document is that if you have a way to reset incomplete events as you re-visualize the components (for example, takeRecords to MutationObserver ), you can make sure that the document is redrawn from top to bottom, and not from bottom to convert rendering complexity to linear from polynomial.
In addition, there is no dangerous period between the initial render and when the registrar is registered, where the Store can change without starting the render, as a result of which the view is not synchronized with the Store (an example of the problem asked in the original question). If the listener is registered in componentDidMount , you need to either make sure that the Store is not changed in calls to componentDidMount in children, or force to re-render / re-synchronize after registering the listener, which if done in componentDidMount is performed in the reverse order, which can be polynomial complexity ( depending on how / if the React setStates aggregated).
Parakleta
source share