@@ -36,18 +36,7 @@ class FixedDataTableContainer extends React.Component {
36
36
constructor ( props ) {
37
37
super ( props ) ;
38
38
39
- this . reduxStore = FixedDataTableStore . get ( ) ;
40
-
41
- this . scrollActions = getScrollActions ( this . reduxStore , ( ) => this . props ) ;
42
-
43
- this . reduxStore . dispatch ( initialize ( props ) ) ;
44
-
45
- this . unsubscribe = this . reduxStore . subscribe ( this . onStoreUpdate . bind ( this ) ) ;
46
- this . state = {
47
- boundState : FixedDataTableContainer . getBoundState ( this . reduxStore ) , // the state from the redux store
48
- reduxStore : this . reduxStore , // put store instance in local state so that getDerivedStateFromProps can access it
49
- props, // put props in local state so that getDerivedStateFromProps can access it
50
- } ;
39
+ this . _initialize ( props ) ;
51
40
52
41
this . fixedDataTableApi = createApi ( ) ;
53
42
this . previousApiValue = null ;
@@ -59,6 +48,10 @@ class FixedDataTableContainer extends React.Component {
59
48
'You must set either a height or a maxHeight'
60
49
) ;
61
50
51
+ if ( ! currentState ) {
52
+ return null ;
53
+ }
54
+
62
55
// getDerivedStateFromProps is called for both prop and state updates.
63
56
// If props are unchanged here, then there's no need to recalculate derived state.
64
57
if ( nextProps === currentState . props ) {
@@ -83,18 +76,57 @@ class FixedDataTableContainer extends React.Component {
83
76
} ;
84
77
}
85
78
79
+ componentDidMount ( ) {
80
+ this . _initialize ( this . props ) ;
81
+ }
82
+
86
83
componentWillUnmount ( ) {
87
- if ( this . unsubscribe ) {
88
- this . unsubscribe ( ) ;
89
- this . unsubscribe = null ;
90
- }
91
- this . reduxStore = null ;
84
+ this . _cleanup ( ) ;
92
85
}
93
86
94
87
componentDidUpdate ( ) {
95
88
this . notifyApiValueChanges ( ) ;
96
89
}
97
90
91
+ _initialize ( props ) {
92
+ if ( this . reduxStore ) {
93
+ // already initialized
94
+ return ;
95
+ }
96
+
97
+ this . reduxStore = FixedDataTableStore . get ( ) ;
98
+ this . reduxStore . dispatch ( initialize ( props ) ) ;
99
+
100
+ this . scrollActions = getScrollActions ( this . reduxStore , ( ) => this . props ) ;
101
+
102
+ this . unsubscribe = this . reduxStore . subscribe ( this . onStoreUpdate . bind ( this ) ) ;
103
+
104
+ const state = {
105
+ boundState : FixedDataTableContainer . getBoundState ( this . reduxStore ) , // the state from the redux store
106
+ reduxStore : this . reduxStore , // put store instance in local state so that getDerivedStateFromProps can access it
107
+ props, // put props in local state so that getDerivedStateFromProps can access it
108
+ } ;
109
+
110
+ if ( this . state ) {
111
+ // component is already constructed, so just update state
112
+ this . setState ( state ) ;
113
+ } else {
114
+ // component isn't constructed yet, so initialize state
115
+ this . state = state ;
116
+ }
117
+ }
118
+
119
+ _cleanup ( ) {
120
+ if ( ! this . reduxStore ) {
121
+ // already cleaned up
122
+ return ;
123
+ }
124
+
125
+ this . unsubscribe ( ) ;
126
+ this . unsubscribe = null ;
127
+ this . reduxStore = null ;
128
+ }
129
+
98
130
/**
99
131
* Returns FDT's public API.
100
132
*
0 commit comments