Skip to content

Commit

Permalink
chore: startTransitionの導入
Browse files Browse the repository at this point in the history
  • Loading branch information
atzzCokeK committed Sep 13, 2024
1 parent 6966078 commit c4f86c3
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 4 deletions.
9 changes: 6 additions & 3 deletions packages/smarthr-ui/src/components/Textarea/Textarea.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import React, {
ComponentPropsWithRef,
forwardRef,
startTransition,
useCallback,
useEffect,
useImperativeHandle,
Expand Down Expand Up @@ -140,12 +141,14 @@ export const Textarea = forwardRef<HTMLTextAreaElement, Props & ElementProps>(
// eslint-disable-next-line react-hooks/exhaustive-deps
const debouncedUpdateCount = useCallback(
debounce((value) => {
setCount(getStringLength(value))
startTransition(() => {
setCount(getStringLength(value))
})
}, 200),
[],
)

const handleOnChange = useCallback(
const handleChange = useCallback(
(event: React.ChangeEvent<HTMLTextAreaElement>) => {
onChange && onChange(event)
maxLetters && debouncedUpdateCount(event.currentTarget.value)
Expand Down Expand Up @@ -197,7 +200,7 @@ export const Textarea = forwardRef<HTMLTextAreaElement, Props & ElementProps>(
{...props}
{...textareaStyleProps}
aria-describedby={actualMaxLettersId}
onChange={handleOnChange}
onChange={handleChange}
ref={textareaRef}
aria-invalid={error || undefined}
rows={interimRows}
Expand Down
2 changes: 1 addition & 1 deletion packages/smarthr-ui/src/libs/debounce.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export const debounce = <T extends (...args: any[]) => void>(
func: T,
wait: number,
): ((...args: Parameters<T>) => void) => {
let timeoutId: NodeJS.Timeout | string | number | null = null
let timeoutId: ReturnType<typeof setTimeout> | null = null

return function (...args: Parameters<T>) {
if (timeoutId !== null) {
Expand Down

0 comments on commit c4f86c3

Please sign in to comment.