From b904f2e71e7504b354c8d54e672652cbc6af2d57 Mon Sep 17 00:00:00 2001 From: rusconn Date: Sat, 24 Jun 2023 14:49:01 +0900 Subject: [PATCH] refactor: memoize input and textarea on export --- app/generators/uuid/page.tsx | 36 ++++++++++++++++-------------------- components/ui/input.tsx | 29 +++++++++++++++-------------- components/ui/textarea.tsx | 7 +++++-- 3 files changed, 36 insertions(+), 36 deletions(-) diff --git a/app/generators/uuid/page.tsx b/app/generators/uuid/page.tsx index a4497e5..a725a02 100644 --- a/app/generators/uuid/page.tsx +++ b/app/generators/uuid/page.tsx @@ -1,6 +1,6 @@ "use client"; -import { useCallback, useMemo, useState } from "react"; +import { useCallback, useState } from "react"; import { range } from "fp-ts/NonEmptyArray"; import * as t from "io-ts"; @@ -54,13 +54,16 @@ export default function Page() { } }, []); - const onGeneratesChange: InputProps["onChange"] = ({ currentTarget: { value } }) => { - const newGenerates = Number(value); + const onGeneratesChange: NonNullable = useCallback( + ({ currentTarget: { value } }) => { + const newGenerates = Number(value); - if (newGenerates >= 1 && newGenerates <= 1000) { - setGenerates(newGenerates); - } - }; + if (newGenerates >= 1 && newGenerates <= 1000) { + setGenerates(newGenerates); + } + }, + [] + ); const onGenerateClick = () => { const newUuids = range(1, generates).map(_ => uuid(uuidVersion, hyphens, uppercase)); @@ -121,18 +124,6 @@ export default function Page() { /> ); - const generatesInput = useMemo( - () => ( - - ), - [generates] - ); - const uuidsCopyButton = ; const uuidsClearButton = ; @@ -149,7 +140,12 @@ export default function Page() { Generate UUID(s) × - {generatesInput} + diff --git a/components/ui/input.tsx b/components/ui/input.tsx index 053f0d3..a93b9f2 100644 --- a/components/ui/input.tsx +++ b/components/ui/input.tsx @@ -1,20 +1,21 @@ import * as React from "react"; +import equal from "react-fast-compare"; import { cn } from "@/lib/style"; export type InputProps = React.InputHTMLAttributes; -export const Input = React.forwardRef( - ({ className, ...props }, ref) => ( - - ) -); -Input.displayName = "Input"; +const RawInput = React.forwardRef(({ className, ...props }, ref) => ( + +)); +RawInput.displayName = "RawInput"; + +export const Input = React.memo(RawInput, equal); diff --git a/components/ui/textarea.tsx b/components/ui/textarea.tsx index 3f2a0d8..6706449 100644 --- a/components/ui/textarea.tsx +++ b/components/ui/textarea.tsx @@ -1,10 +1,11 @@ import * as React from "react"; +import equal from "react-fast-compare"; import { cn } from "@/lib/style"; export type TextareaProps = React.TextareaHTMLAttributes; -export const Textarea = React.forwardRef( +export const RawTextarea = React.forwardRef( ({ className, ...props }, ref) => (