Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove unnecessary useEffect hook #750

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 20 additions & 22 deletions src/number_format_base.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -69,28 +69,6 @@ export default function NumberFormatBase<BaseType = InputAttributes>(
onFormattedValueChange(values, source);
};

// check if there is any change in the value due to props change
useEffect(() => {
const newFormattedValue = (format as FormatInputValueFunction)(numAsString);

// if the formatted value is not synced to parent, or if the formatted value is different
if (lastUpdatedValue.current === undefined || newFormattedValue !== lastUpdatedValue.current) {
const input = focusedElm.current;

// formatting can remove some of the number chars, so we need to fine number string again
const _numAsString = removeFormatting(newFormattedValue, undefined);

updateValue({
formattedValue: newFormattedValue,
numAsString: _numAsString,
input,
setCaretPosition: true,
source: SourceType.props,
event: undefined,
});
}
});

const [mounted, setMounted] = useState(false);
const focusedElm = useRef<HTMLInputElement | null>(null);

Expand Down Expand Up @@ -219,6 +197,26 @@ export default function NumberFormatBase<BaseType = InputAttributes>(
}
};

// check if there is any change in the value due to props change
const newFormattedValue = (format as FormatInputValueFunction)(numAsString);

// if the formatted value is not synced to parent, or if the formatted value is different
if (lastUpdatedValue.current === undefined || newFormattedValue !== lastUpdatedValue.current) {
const input = focusedElm.current;

// formatting can remove some of the number chars, so we need to fine number string again
const _numAsString = removeFormatting(newFormattedValue, undefined);

updateValue({
formattedValue: newFormattedValue,
numAsString: _numAsString,
input,
setCaretPosition: true,
source: SourceType.props,
event: undefined,
});
}

const formatInputValue = (
inputValue: string,
event:
Expand Down