Last active
November 20, 2017 14:07
-
-
Save stepankuzmin/eb6f2efac20ef79b5ec0ee56f17844bc to your computer and use it in GitHub Desktop.
shallowCompare React Children
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // @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