[Perf] Some optimizations

This commit is contained in:
Alexander Zinchuk 2021-05-09 01:59:58 +03:00
parent 4e0f4d99e8
commit 3ac7711f17
2 changed files with 19 additions and 5 deletions

View File

@ -13,18 +13,18 @@ export default (
const [isClosed, setIsClosed] = useState(!isOpen);
const closeTimeoutRef = useRef<number>();
// СSS class should be added in a separate tick to turn on CSS transition.
const [hasAsyncOpenClassName, setHasAsyncOpenClassName] = useState(false);
const [hasOpenClassName, setHasOpenClassName] = useState(isOpen && noOpenTransition);
if (isOpen) {
setIsClosed(false);
setHasAsyncOpenClassName(true);
setHasOpenClassName(true);
if (closeTimeoutRef.current) {
window.clearTimeout(closeTimeoutRef.current);
closeTimeoutRef.current = undefined;
}
} else {
setHasAsyncOpenClassName(false);
setHasOpenClassName(false);
if (!isClosed && !closeTimeoutRef.current) {
closeTimeoutRef.current = window.setTimeout(() => {
@ -39,7 +39,6 @@ export default (
}
}
const hasOpenClassName = hasAsyncOpenClassName || (isOpen && noOpenTransition);
const isClosing = Boolean(closeTimeoutRef.current);
const shouldRender = isOpen || isClosing;
const transitionClassNames = buildClassName(

View File

@ -1,4 +1,8 @@
export default function arePropsShallowEqual(currentProps: AnyLiteral, newProps: AnyLiteral) {
if (currentProps === newProps) {
return true;
}
const currentKeys = Object.keys(currentProps);
const currentKeysLength = currentKeys.length;
const newKeysLength = Object.keys(newProps).length;
@ -7,5 +11,16 @@ export default function arePropsShallowEqual(currentProps: AnyLiteral, newProps:
return false;
}
return currentKeys.every((prop) => currentProps[prop] === newProps[prop]);
if (currentKeysLength === 0) {
return true;
}
for (let i = 0; i < currentKeysLength; i++) {
const prop = currentKeys[i];
if (currentProps[prop] !== newProps[prop]) {
return false;
}
}
return true;
}