-
Notifications
You must be signed in to change notification settings - Fork 241
NOTICE: componentWillMount and this starter kit #409
Description
We use react-async-bootstrapper
to bootstrap your application on the client/server sides so that things like async components (and data in the features/react-jobs
branch) can be pre-resolved.
Your React application will go through the following process:
- Passed into
asyncBootstrapper
- this tries to mimic the behaviour of a server side render (to a degree) by walking your react component tree. It has to callcomponentWillMount
along the way as this is expected behaviour inrenderToString/renderToStaticMarkup
calls. Generally people put things like state initialisation etc in thecomponentWillMount
, so this will be needed so that therender
functions of your components can be called with the required state it needs to return it's result (i.e. children). The process continues down the "tree" until the bootstrapping process is complete. This may sound expensive but all we are doing is creating and throwing away object instances - no DOM creation/interaction/manipulation occurs. - After the bootstrapping process has complete your application is passed to the
render
/renderToString
functions for "normal" rendering (with all the global state given to it by the bootstrapping process).
I have found the above working awesomely for me in large production projects that use Redux as a state management tool. However, I have become aware of other libraries (e.g. MobX) that bind some "event listeners" within the componentWillMount
call. This will cause unexpected behaviour given our bootstrapping process. This is because the bootstrapping process never calls componentWillUnmount
, again mimicking the behaviour of a typical renderToString
/renderToStaticMarkup
call. Unfortunately libraries that generally do event binding in componentWillMount
do the unbinding within componentWillUnmount
. As we never call this the event listeners for the bootstrapping process live on, and then things like MobX state updates will cause things like forceUpdate
errors as the associated components do not exist.
I am considering extending the asyncBootstrapper
function to allow you to pass a prop stating that componentWillUnmount
should run. This may improve interoperability with these types of libraries, however, it could cause issues on others. We will have to experiment together.