Add memo to components

Add suspense for dynamic import
This commit is contained in:
DefectingCat
2022-12-06 09:33:06 +08:00
parent 590e3d2752
commit 7954c974da
39 changed files with 455 additions and 382 deletions

View File

@ -1,4 +1,4 @@
import { AnchorHTMLAttributes, forwardRef } from 'react';
import { AnchorHTMLAttributes, forwardRef, memo } from 'react';
import clsx from 'clsx';
import { FiExternalLink } from 'react-icons/fi';
@ -33,6 +33,7 @@ const Anchor = forwardRef<HTMLAnchorElement, Props>(
);
}
);
Anchor.displayName = 'Anchor';
export default Anchor;
export default memo(Anchor);

View File

@ -1,5 +1,6 @@
import clsx from 'clsx';
import NextImage, { ImageProps } from 'next/image';
import { memo } from 'react';
interface Props extends ImageProps {}
@ -18,4 +19,4 @@ const Image = ({ alt, ...rest }: Props) => {
);
};
export default Image;
export default memo(Image);

View File

@ -1,5 +1,5 @@
import clsx from 'clsx';
import { ReactNode } from 'react';
import { memo, ReactNode } from 'react';
import { FiExternalLink } from 'react-icons/fi';
interface Props {
@ -26,4 +26,4 @@ const Anchor = ({ children, external = true }: Props) => {
);
};
export default Anchor;
export default memo(Anchor);

View File

@ -1,8 +1,13 @@
import clsx from 'clsx';
import CopyButton from 'components/post/CopyButton';
import useCopyToClipboard from 'lib/hooks/useCopyToClipboard';
import { DetailedHTMLProps, HTMLAttributes, lazy, useRef } from 'react';
const CopyButton = lazy(() => import('components/post/CopyButton'));
import {
DetailedHTMLProps,
HTMLAttributes,
memo,
useCallback,
useRef,
} from 'react';
type Props = {} & DetailedHTMLProps<
HTMLAttributes<HTMLPreElement>,
@ -14,11 +19,11 @@ const Pre = ({ ...rest }: Props) => {
const preRef = useRef<HTMLPreElement>(null);
const { copy } = useCopyToClipboard();
const handleCopy = () => {
const handleCopy = useCallback(() => {
if (!preRef.current) throw new Error('Can not access pre element.');
if (preRef.current.textContent == null) return;
copy(preRef.current.textContent);
};
}, [copy]);
return (
<>
@ -34,4 +39,4 @@ const Pre = ({ ...rest }: Props) => {
);
};
export default Pre;
export default memo(Pre);