Skip to content

Commit

Permalink
Fix useCountUp unnecessary updates (#547)
Browse files Browse the repository at this point in the history
* Fix useCountUp unnecessary updates

* Some codestyle improvements
  • Loading branch information
mmarkelov committed Sep 9, 2021
1 parent 44fdffd commit 26ed8ac
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 47 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
],
"typings": "build/index.d.ts",
"scripts": {
"format": "prettier --write \"*.md\" \"src/**/*.ts\"",
"build": "rollup -c && tsc --emitDeclarationOnly --noEmit false --project src/tsconfig.json --outDir build",
"prepublishOnly": "yarn build",
"test": "jest"
Expand Down
28 changes: 15 additions & 13 deletions src/useCountUp.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { CallbackProps, CommonProps, CountUpApi, UpdateFn } from './types';
import { useMemo, useRef, useEffect } from 'react';
import React, { useMemo, useRef, useEffect } from 'react';
import { createCountUpInstance } from './common';
import { useEventCallback } from './helpers/useEventCallback';
import { CountUp as CountUpJs } from 'countup.js';
Expand Down Expand Up @@ -40,7 +40,6 @@ const useCountUp = (props: useCountUpProps): CountUpApi => {

const createInstance = useEventCallback(() => {
return createCountUpInstance(
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
typeof ref === 'string' ? ref : ref.current!,
instanceProps,
);
Expand Down Expand Up @@ -96,15 +95,11 @@ const useCountUp = (props: useCountUpProps): CountUpApi => {
start();
});

const maybeInitialize = useEventCallback(() => {
const maybeInitialize = useEventCallback((shouldReset?: boolean) => {
if (startOnMount) {
start();
}
});

const maybeReinitialize = useEventCallback(() => {
if (startOnMount) {
reset();
if (shouldReset) {
reset();
}
start();
}
});
Expand All @@ -115,14 +110,21 @@ const useCountUp = (props: useCountUpProps): CountUpApi => {

maybeInitialize();
} else if (enableReinitialize) {
maybeReinitialize();
maybeInitialize(true);
}
}, [
enableReinitialize,
isInitializedRef,
maybeInitialize,
maybeReinitialize,
props,
delay,
props.start,
props.suffix,
props.prefix,
props.duration,
props.separator,
props.decimals,
props.decimal,
props.formattingFn,
]);

useEffect(() => {
Expand Down
34 changes: 0 additions & 34 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1532,14 +1532,6 @@
"@typescript-eslint/typescript-estree" "4.30.0"
debug "^4.3.1"

"@typescript-eslint/scope-manager@4.29.3":
version "4.29.3"
resolved "https://registry.yarnpkg.com/@typescript-eslint/scope-manager/-/scope-manager-4.29.3.tgz#497dec66f3a22e459f6e306cf14021e40ec86e19"
integrity sha512-x+w8BLXO7iWPkG5mEy9bA1iFRnk36p/goVlYobVWHyDw69YmaH9q6eA+Fgl7kYHmFvWlebUTUfhtIg4zbbl8PA==
dependencies:
"@typescript-eslint/types" "4.29.3"
"@typescript-eslint/visitor-keys" "4.29.3"

"@typescript-eslint/scope-manager@4.30.0":
version "4.30.0"
resolved "https://registry.yarnpkg.com/@typescript-eslint/scope-manager/-/scope-manager-4.30.0.tgz#1a3ffbb385b1a06be85cd5165a22324f069a85ee"
Expand All @@ -1548,29 +1540,11 @@
"@typescript-eslint/types" "4.30.0"
"@typescript-eslint/visitor-keys" "4.30.0"

"@typescript-eslint/types@4.29.3":
version "4.29.3"
resolved "https://registry.yarnpkg.com/@typescript-eslint/types/-/types-4.29.3.tgz#d7980c49aef643d0af8954c9f14f656b7fd16017"
integrity sha512-s1eV1lKNgoIYLAl1JUba8NhULmf+jOmmeFO1G5MN/RBCyyzg4TIOfIOICVNC06lor+Xmy4FypIIhFiJXOknhIg==

"@typescript-eslint/types@4.30.0":
version "4.30.0"
resolved "https://registry.yarnpkg.com/@typescript-eslint/types/-/types-4.30.0.tgz#fb9d9b0358426f18687fba82eb0b0f869780204f"
integrity sha512-YKldqbNU9K4WpTNwBqtAerQKLLW/X2A/j4yw92e3ZJYLx+BpKLeheyzoPfzIXHfM8BXfoleTdiYwpsvVPvHrDw==

"@typescript-eslint/typescript-estree@4.29.3":
version "4.29.3"
resolved "https://registry.yarnpkg.com/@typescript-eslint/typescript-estree/-/typescript-estree-4.29.3.tgz#1bafad610015c4ded35c85a70b6222faad598b40"
integrity sha512-45oQJA0bxna4O5TMwz55/TpgjX1YrAPOI/rb6kPgmdnemRZx/dB0rsx+Ku8jpDvqTxcE1C/qEbVHbS3h0hflag==
dependencies:
"@typescript-eslint/types" "4.29.3"
"@typescript-eslint/visitor-keys" "4.29.3"
debug "^4.3.1"
globby "^11.0.3"
is-glob "^4.0.1"
semver "^7.3.5"
tsutils "^3.21.0"

"@typescript-eslint/typescript-estree@4.30.0":
version "4.30.0"
resolved "https://registry.yarnpkg.com/@typescript-eslint/typescript-estree/-/typescript-estree-4.30.0.tgz#ae57833da72a753f4846cd3053758c771670c2ac"
Expand All @@ -1584,14 +1558,6 @@
semver "^7.3.5"
tsutils "^3.21.0"

"@typescript-eslint/visitor-keys@4.29.3":
version "4.29.3"
resolved "https://registry.yarnpkg.com/@typescript-eslint/visitor-keys/-/visitor-keys-4.29.3.tgz#c691760a00bd86bf8320d2a90a93d86d322f1abf"
integrity sha512-MGGfJvXT4asUTeVs0Q2m+sY63UsfnA+C/FDgBKV3itLBmM9H0u+URcneePtkd0at1YELmZK6HSolCqM4Fzs6yA==
dependencies:
"@typescript-eslint/types" "4.29.3"
eslint-visitor-keys "^2.0.0"

"@typescript-eslint/visitor-keys@4.30.0":
version "4.30.0"
resolved "https://registry.yarnpkg.com/@typescript-eslint/visitor-keys/-/visitor-keys-4.30.0.tgz#a47c6272fc71b0c627d1691f68eaecf4ad71445e"
Expand Down

0 comments on commit 26ed8ac

Please sign in to comment.