Skip to content

Commit 41cd350

Browse files
Fix StrictMode errors in FixedDataTableContainer
1 parent d9575f6 commit 41cd350

File tree

1 file changed

+49
-17
lines changed

1 file changed

+49
-17
lines changed

Diff for: src/FixedDataTableContainer.js

+49-17
Original file line numberDiff line numberDiff line change
@@ -36,18 +36,7 @@ class FixedDataTableContainer extends React.Component {
3636
constructor(props) {
3737
super(props);
3838

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);
5140

5241
this.fixedDataTableApi = createApi();
5342
this.previousApiValue = null;
@@ -59,6 +48,10 @@ class FixedDataTableContainer extends React.Component {
5948
'You must set either a height or a maxHeight'
6049
);
6150

51+
if (!currentState) {
52+
return null;
53+
}
54+
6255
// getDerivedStateFromProps is called for both prop and state updates.
6356
// If props are unchanged here, then there's no need to recalculate derived state.
6457
if (nextProps === currentState.props) {
@@ -83,18 +76,57 @@ class FixedDataTableContainer extends React.Component {
8376
};
8477
}
8578

79+
componentDidMount() {
80+
this._initialize(this.props);
81+
}
82+
8683
componentWillUnmount() {
87-
if (this.unsubscribe) {
88-
this.unsubscribe();
89-
this.unsubscribe = null;
90-
}
91-
this.reduxStore = null;
84+
this._cleanup();
9285
}
9386

9487
componentDidUpdate() {
9588
this.notifyApiValueChanges();
9689
}
9790

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+
98130
/**
99131
* Returns FDT's public API.
100132
*

0 commit comments

Comments
 (0)