Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf: use shouldComponentUpdate instead of PureComponent in TableRow #289

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions src/TableRow.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import React from 'react';
import PropTypes from 'prop-types';

import { renderElement } from './utils';
import { isPropsShallowEqual, renderElement } from './utils';

/**
* Row component for BaseTable
*/
class TableRow extends React.PureComponent {
class TableRow extends React.Component {
constructor(props) {
super(props);

Expand All @@ -18,6 +18,10 @@ class TableRow extends React.PureComponent {
this._handleExpand = this._handleExpand.bind(this);
}

shouldComponentUpdate(newProps) {
return !isPropsShallowEqual(this.props, newProps);
Copy link

@showme79 showme79 Mar 2, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

state change (eg. row measuring progress) is not detected here:

shouldComponentUpdate(newProps, newState) {
    return newState !== this.state && !isPropsShallowEqual(this.props, newProps);
}

}

componentDidMount() {
this.props.estimatedRowHeight && this.props.rowIndex >= 0 && this._measureHeight(true);
}
Expand Down
24 changes: 24 additions & 0 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,30 @@ export function isObjectEqual(objA, objB, ignoreFunction = true) {
return true;
}

const hasOwnProperty = Object.prototype.hasOwnProperty;
export function isPropsShallowEqual(objA, objB) {
if (objA === objB) return true;
if (objA === null || objB === null) return false;
if (typeof objA !== 'object' || typeof objB !== 'object') return false;

const keysA = Object.keys(objA);
const keysB = Object.keys(objB);
if (keysA.length !== keysB.length) return false;

for (let i = 0; i < keysA.length; i++) {
const key = keysA[i];
const valueA = objA[key];
if (
(!hasOwnProperty.call(objB, key) || valueA !== objB[key]) &&
(key !== 'style' || !isObjectEqual(valueA, objB[key]))
) {
return false;
}
}

return true;
}

export function callOrReturn(funcOrValue, ...args) {
return typeof funcOrValue === 'function' ? funcOrValue(...args) : funcOrValue;
}
Expand Down