@@ -7,6 +7,10 @@ import site from './site';
77class EntityMapper extends Component {
88 static entityComponents = new Map ( ) ;
99
10+ static contextTypes = {
11+ hnContext : PropTypes . object ,
12+ } ;
13+
1014 /**
1115 * This makes sure the data for this url is ready to be rendered.
1216 * @param uuid
@@ -62,8 +66,30 @@ class EntityMapper extends Component {
6266 } ;
6367 }
6468
65- componentDidMount ( ) {
66- this . loadComponent ( this . props ) ;
69+ /**
70+ * If this component exists in a tree that is invoked with the waitForHnData function, this function is invoked.
71+ * Only after the promise is resolved, the component will be mounted. To keep the data fetched here, we assign the
72+ * state to the hnContext provided by the DrupalPageContextProvider. This way, the state will be preserved trough
73+ * multiple renders.
74+ */
75+ async asyncBootstrap ( ) {
76+ const { mapper, asyncMapper } = this . props ;
77+ const { uuid, entityProps } = this . state ;
78+ this . context . hnContext . state . entities [ uuid ] = await this . loadComponent ( { uuid, mapper, asyncMapper, entityProps } ) ;
79+ return true ;
80+ }
81+
82+ /**
83+ * The first time this element is rendered, we always make sure the component and the Drupal page is loaded.
84+ */
85+ componentWillMount ( ) {
86+ const { uuid } = this . props ;
87+ const state = getNested ( ( ) => this . context . hnContext . state . entities [ uuid ] ) ;
88+ if ( state ) {
89+ this . setState ( state ) ;
90+ } else {
91+ this . loadComponent ( this . props ) ;
92+ }
6793 }
6894
6995 componentWillReceiveProps ( nextProps ) {
@@ -75,7 +101,11 @@ class EntityMapper extends Component {
75101 async loadComponent ( { uuid, mapper, asyncMapper, entityProps } ) {
76102 this . setState ( { ready : false } ) ;
77103 const entityComponentSymbol = await EntityMapper . assureComponent ( { uuid, mapper, asyncMapper } ) ;
78- this . setState ( { uuid, entityComponentSymbol, ready : true , entityProps } ) ;
104+
105+ const newState = { ...this . state , ...{ uuid, entityComponentSymbol, ready : true , entityProps } } ;
106+ this . setState ( newState ) ;
107+
108+ return newState ;
79109 }
80110
81111 isReady ( ) {
0 commit comments