Skip to content

Commit 7da845d

Browse files
authored
feat: add ignoreFunctionInColumnCompare to solve closure problem in renderers (#213)
* feat: add ignoreFunctionInColumnCompare to solve closure problem in renderers * add example
1 parent e7d9884 commit 7da845d

File tree

4 files changed

+55
-20
lines changed

4 files changed

+55
-20
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
## NEXT VERSION
44

5+
- feat: add `ignoreFunctionInColumnCompare` to solve closure problem in renderers
6+
57
## v1.10.9 (2020-08-13)
68

79
- fix: input loses focus on unmount

README.md

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,16 +34,32 @@ import 'react-base-table/styles.css'
3434

3535
Learn more at the [website](https://autodesk.github.io/react-base-table/)
3636

37-
**Make sure each item in `data` is unique by a key, the default key is `id`, you can customize it via `rowKey`**
37+
### unique key
3838

39-
**`key` is required for column definition or the column will be ignored**
39+
`key` is required for column definition or the column will be ignored
4040

41-
**`width` and `height`(or `maxHeight`) are required to display the table properly**
41+
Make sure each item in `data` is unique by a key, the default key is `id`, you can customize it via `rowKey`
42+
43+
### size
44+
45+
`width` is required for column definition, but in flex mode(`fixed={false}`), you can set `width={0}` and `flexGrow={1}` to achieve flexible column width, checkout the [Flex Column](https://autodesk.github.io/react-base-table/examples/flex-column) example
46+
47+
`width` and `height`(or `maxHeight`) are required to display the table properly
4248

4349
In the [examples](https://autodesk.github.io/react-base-table/examples)
4450
we are using a wrapper `const Table = props => <BaseTable width={700} height={400} {...props} />` to do that
4551

46-
If you want it responsive, you can use the [`AutoResizer`](https://autodesk.github.io/react-base-table/api/autoresizer) to make the table fill the container, checkout the [Auto Resize example](https://autodesk.github.io/react-base-table/examples/auto-resize)
52+
If you want it responsive, you can use the [`AutoResizer`](https://autodesk.github.io/react-base-table/api/autoresizer) to make the table fill the container, checkout the [Auto Resize](https://autodesk.github.io/react-base-table/examples/auto-resize) example
53+
54+
### closure problem in custom renderers
55+
56+
In practice we tend to write inline functions for custom renderers, which would make `shouldUpdateComponent` always true as the inline function will create a new instance on every re-render, to avoid "unnecessary" re-renders, **`BaseTable` ignores functions when comparing column definition by default**, it works well in most cases before, but if we use external data instead of reference state in custom renderers, we always get the staled initial value although the data has changed
57+
58+
It's recommended to inject the external data in column definition to solve the problem, like `<Column foo={foo} bar={bar} cellRenderer={({ column: { foo, bar }}) => { ... } } />`, the column definition will update on external data change, with this pattern we can easily move the custom renderers out of column definition for sharing, the downside is it would bloat the column definition and bug prone
59+
60+
Things getting worse with the introduction of React hooks, we use primitive state instead of `this.state`, so it's easy to encounter the closure problem, but with React hooks, we can easily memoize functions via `useCallback` or `useMemo`, so the implicit optimization could be replaced with user land optimization which is more intuitive, to turn off the implicit optimization, set `ignoreFunctionInColumnCompare` to `false` which is introduced since `v1.11.0`
61+
62+
Here is an [example](https://autodesk.github.io/react-base-table/playground#MYewdgzgLgBKA2BXAtpGBeGBzApmHATgIZQ4DCISqEAFAIwAMAlAFCiSwAmJRG2ehEjgAiPGghSQANDABMDZixY4AHgAcQBLjgBmRRPFg0mGAHwwA3ixhxw0GAG1QiMKQIyIOKBRduAunwASjhEwFAAdIieAMpQQjSKNuz2DgCWWGCaOABiLmGp4B5eAJIZWblg+eABmMGhEVE4sfFQBIg4rEl2sGlgAFY4YRRUYEVQxf2D3pSSNTB1YZExcaQ0evCenTAEXogEYDA01jYwADymxydnnKkAbjDQAJ7wOOgWFjBqRJw3YFgAXDAACwyG4QNTwIiPQEAch0LxUMJkfSiUFSOkeFFceCgsPBoRwAFoAEZeADuODwMJgAF8aRcrldTsTEFAoOAYOAyPBUsAANZvYxmB5eHzYgjiEC+QgwADUMDoTHpstOAHoWWzwAzGTZTpDSfBtTrdakwGpWZdjTYoI81K8AETAAAWgz5xJAKntlqtztdOE4b3SmR2FSqYBp3uNXKdRD+rwsOGFnnGZRDeTR4BoOHCcQIuAivv5-qVkauqqNxtKwcTOnTBQOptsI1syC+O1LZ1V+pwho7eqIBorOtOpvNUA7VxtdvQjpd-PdnonJ0LfP9gcmQxmqAjVqu0djuDeifQ5mTEwGm5GWZzRDzXnCK+LO935aX56mMFUrV43DiMHZTaSDAnC6KaqQZmAfZdgOPZDp2Ny3HBpwACoDi8MA6KkKj+sBPBvL+RA0jAQblHW4ATMMkgUK2t7xiRaaVBB9J9pRqBLhY4ScRI1AOAwfjPlaBEAOJeG4gomCetjSgQAnGs44rrhe0zNgA-FJ4owICLggZh+CcLJZZwbqrEHBxXFbpADh0PxMCvlapwmZYnEPhZEAOLINl2caDkWU55kjG5ADMnlGWcjlmS5AUOECIWRmqqHEi8FZqtqrARkAA) to demonstrate
4763

4864
## Browser Support
4965

@@ -117,7 +133,7 @@ We are using a advanced table component based on `BaseTable` internally, with mu
117133

118134
![AdvanceTable](screenshots/advance-table.png)
119135

120-
[In real products](https://blogs.autodesk.com/bim360-release-notes/2019/11/18/bim-360-cost-management-update-november-2019/)
136+
[In real products](https://blogs.autodesk.com/bim360-release-notes/2019/11/18/bim-360-cost-management-update-november-2019/)
121137

122138
## Development
123139

src/BaseTable.js

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -97,18 +97,21 @@ class BaseTable extends React.PureComponent {
9797
this._depthMap = {};
9898
return flattenOnKeys(tree, keys, this._depthMap, dataKey);
9999
});
100-
this._resetColumnManager = memoize((columns, fixed) => {
101-
this.columnManager.reset(columns, fixed);
100+
this._resetColumnManager = memoize(
101+
(columns, fixed) => {
102+
this.columnManager.reset(columns, fixed);
102103

103-
if (this.props.estimatedRowHeight && fixed) {
104-
if (!this.columnManager.hasLeftFrozenColumns()) {
105-
this._leftRowHeightMap = {};
106-
}
107-
if (!this.columnManager.hasRightFrozenColumns()) {
108-
this._rightRowHeightMap = {};
104+
if (this.props.estimatedRowHeight && fixed) {
105+
if (!this.columnManager.hasLeftFrozenColumns()) {
106+
this._leftRowHeightMap = {};
107+
}
108+
if (!this.columnManager.hasRightFrozenColumns()) {
109+
this._rightRowHeightMap = {};
110+
}
109111
}
110-
}
111-
}, isObjectEqual);
112+
},
113+
(newArgs, lastArgs) => isObjectEqual(newArgs, lastArgs, this.props.ignoreFunctionInColumnCompare)
114+
);
112115

113116
this._isResetting = false;
114117
this._resetIndex = null;
@@ -1033,6 +1036,7 @@ BaseTable.defaultProps = {
10331036
overscanRowCount: 1,
10341037
onEndReachedThreshold: 500,
10351038
getScrollbarSize: defaultGetScrollbarSize,
1039+
ignoreFunctionInColumnCompare: true,
10361040

10371041
onScroll: noop,
10381042
onRowsRendered: noop,
@@ -1283,6 +1287,10 @@ BaseTable.propTypes = {
12831287
* Each of the handlers is of the shape of `({ rowData, rowIndex, rowKey, event }) => object`
12841288
*/
12851289
rowEventHandlers: PropTypes.object,
1290+
/**
1291+
* whether to ignore function properties while comparing column definition
1292+
*/
1293+
ignoreFunctionInColumnCompare: PropTypes.bool,
12861294
/**
12871295
* A object for the custom components, like `ExpandIcon` and `SortIndicator`
12881296
*/

src/utils.js

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ export function normalizeColumns(elements) {
2626
return columns;
2727
}
2828

29-
export function isObjectEqual(objA, objB) {
29+
export function isObjectEqual(objA, objB, ignoreFunction = true) {
3030
if (objA === objB) return true;
3131
if (objA === null && objB === null) return true;
3232
if (objA === null || objB === null) return false;
@@ -38,13 +38,22 @@ export function isObjectEqual(objA, objB) {
3838

3939
for (let i = 0; i < keysA.length; i++) {
4040
const key = keysA[i];
41+
42+
if (key === '_owner' && objA.$$typeof) {
43+
// React-specific: avoid traversing React elements' _owner.
44+
// _owner contains circular references
45+
// and is not needed when comparing the actual elements (and not their owners)
46+
continue;
47+
}
48+
4149
const valueA = objA[key];
4250
const valueB = objB[key];
51+
const valueAType = typeof valueA;
4352

44-
if (typeof valueA !== typeof valueB) return false;
45-
if (typeof valueA === 'function') continue;
46-
if (typeof valueA === 'object') {
47-
if (!isObjectEqual(valueA, valueB)) return false;
53+
if (valueAType !== typeof valueB) return false;
54+
if (valueAType === 'function' && ignoreFunction) continue;
55+
if (valueAType === 'object') {
56+
if (!isObjectEqual(valueA, valueB, ignoreFunction)) return false;
4857
else continue;
4958
}
5059
if (valueA !== valueB) return false;

0 commit comments

Comments
 (0)