Skip to content

Instantly share code, notes, and snippets.

@stepankuzmin
Last active November 20, 2017 14:07
Show Gist options
  • Save stepankuzmin/eb6f2efac20ef79b5ec0ee56f17844bc to your computer and use it in GitHub Desktop.
Save stepankuzmin/eb6f2efac20ef79b5ec0ee56f17844bc to your computer and use it in GitHub Desktop.
shallowCompare React Children
// @flow
import { Children } from 'react';
import type { Node } from 'react';
const childrenKeys = (children: Node): string[] =>
Children.map(children, child => child.key);
const shallowCompareChildren = (prevChildren: Node, newChildren: Node): boolean => {
if (Children.count(prevChildren) !== Children.count(newChildren)) {
return false;
}
const prevKeys = childrenKeys(prevChildren);
const newKeys = new Set(childrenKeys(newChildren));
return prevKeys.every(key => newKeys.has(key));
};
export default shallowCompareChildren;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment