Skip to content
This repository has been archived by the owner on Dec 5, 2024. It is now read-only.

Commit

Permalink
Extracted shallow equals to a utility
Browse files Browse the repository at this point in the history
  • Loading branch information
maclockard committed Sep 17, 2019
1 parent 0ba3ea2 commit 277dae7
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 28 deletions.
12 changes: 6 additions & 6 deletions .size-snapshot.json
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
{
"dist/index.umd.js": {
"bundled": 51976,
"minified": 17998,
"gzipped": 5723
"bundled": 51979,
"minified": 17984,
"gzipped": 5712
},
"dist/index.umd.min.js": {
"bundled": 25153,
"minified": 9859,
"gzipped": 3371
},
"dist/index.esm.js": {
"bundled": 12252,
"minified": 7276,
"gzipped": 2063,
"bundled": 12247,
"minified": 7288,
"gzipped": 2066,
"treeshaked": {
"rollup": {
"code": 3754,
Expand Down
31 changes: 9 additions & 22 deletions src/Popper.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import PopperJS, {
} from 'popper.js';
import type { Style } from 'typed-styles';
import { ManagerContext } from './Manager';
import { safeInvoke, unwrapArray } from './utils';
import { safeInvoke, unwrapArray, shallowEqual } from './utils';

type getRefFn = (?HTMLElement) => void;
type ReferenceElement = ReferenceObject | HTMLElement | null;
Expand Down Expand Up @@ -169,27 +169,14 @@ export class InnerPopper extends React.Component<PopperProps, PopperState> {
) {

// develop only check that modifiers isn't being updated needlessly
if(process.env.NODE_ENV === "development" && this.props.modifiers !== prevProps.modifiers && this.props.modifiers != null && prevProps.modifiers != null) {
let needlessChange = false;

var prevKeys = Object.keys(prevProps.modifiers);
var nowKeys = Object.keys(this.props.modifiers);

if (nowKeys.length !== prevKeys.length) {
needlessChange = true;
}

for (var i = 0; i < nowKeys.length; i++) {
var key = prevKeys[i];

if (this.props.modifiers[key] !== prevProps.modifiers[key]) {
needlessChange = true;
}
}

if (needlessChange === true) {
console.warn("'modifiers' prop reference updated even though all values appear the same.\nConsider memoizing the 'modifiers' object to avoid needless rendering.");
}
if (
process.env.NODE_ENV === "development" &&
this.props.modifiers !== prevProps.modifiers &&
this.props.modifiers != null &&
prevProps.modifiers != null &&
shallowEqual(this.props.modifiers, prevProps.modifiers)
) {
console.warn("'modifiers' prop reference updated even though all values appear the same.\nConsider memoizing the 'modifiers' object to avoid needless rendering.");
}

this.updatePopperInstance();
Expand Down
23 changes: 23 additions & 0 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,26 @@ export const safeInvoke = (fn: ?Function, ...args: *) => {
return fn(...args);
}
}

/**
* Does a shallow equality check of two objects by comparing the reference
* equality of each value.
*/
export const shallowEqual = (objA: { [key: string]: any}, objB: { [key: string]: any}): boolean => {
var aKeys = Object.keys(objA);
var bKeys = Object.keys(objB);

if (bKeys.length !== aKeys.length) {
return false;
}

for (var i = 0; i < bKeys.length; i++) {
var key = aKeys[i];

if (objA[key] !== objB[key]) {
return false;
}
}

return true;
}

0 comments on commit 277dae7

Please sign in to comment.