@@ -42,6 +42,10 @@ export interface GojsModel extends go.Model {
4242class GojsDiagram < N extends BaseNodeModel , L extends LinkModel > extends React . PureComponent < GojsDiagramProps < N , L > >
4343 implements DiagramNotificationDelegate < N , L > {
4444 private myDiagram : Diagram ;
45+ private divRef : React . RefObject < HTMLDivElement > ;
46+
47+ private mountInterval ;
48+
4549 private eventsToDispatch : ModelChangeEvent < N , L > [ ] ;
4650 private modelChangedHandlers = [
4751 new AddNodeModelChangedHandler < N , L > ( ) ,
@@ -55,19 +59,36 @@ class GojsDiagram<N extends BaseNodeModel, L extends LinkModel> extends React.Pu
5559
5660 constructor ( props : GojsDiagramProps < N , L > ) {
5761 super ( props ) ;
62+ this . divRef = React . createRef ( ) ;
5863 this . eventsToDispatch = [ ] ;
5964 this . modelChangedHandler = this . modelChangedHandler . bind ( this ) ;
6065 }
6166
6267 componentDidMount ( ) {
63- this . init ( ) ;
68+ let intervalCount = 0 ;
69+ this . mountInterval = setInterval ( ( ) => {
70+ if ( this . divRef . current && this . divRef . current . clientWidth && this . divRef . current . clientHeight ) {
71+ this . init ( ) ;
72+ clearInterval ( this . mountInterval ) ;
73+ } else {
74+ if ( intervalCount > 10 ) {
75+ clearInterval ( this . mountInterval ) ;
76+ }
77+ intervalCount ++ ;
78+ }
79+ } , 10 ) ;
6480 }
6581
6682 componentWillUnmount ( ) {
67- if ( this . props . onModelChange ) {
83+ if ( this . myDiagram && this . props . onModelChange ) {
6884 this . myDiagram . removeModelChangedListener ( this . modelChangedHandler ) ;
6985 }
70- this . myDiagram . clear ( ) ;
86+ if ( this . myDiagram ) {
87+ this . myDiagram . clear ( ) ;
88+ }
89+ if ( this . mountInterval ) {
90+ clearInterval ( this . mountInterval ) ;
91+ }
7192 }
7293
7394 componentDidUpdate ( ) {
@@ -109,7 +130,7 @@ class GojsDiagram<N extends BaseNodeModel, L extends LinkModel> extends React.Pu
109130 }
110131 }
111132 render ( ) {
112- return < div id = { this . props . diagramId } className = { this . props . className } /> ;
133+ return < div ref = { this . divRef } id = { this . props . diagramId } className = { this . props . className } /> ;
113134 }
114135
115136 enqueueEvent ( event : ModelChangeEvent < N , L > ) {
0 commit comments