From 086da747216b1b3acd5f6ace7aac92d8e05ba625 Mon Sep 17 00:00:00 2001 From: DefectingCat Date: Thu, 25 Aug 2022 17:08:18 +0800 Subject: [PATCH] Remove useless usecallback --- components/NavBar.tsx | 12 ++++++------ components/RUA/RUACodeSandbox.tsx | 6 +++--- components/RUA/tab/index.tsx | 6 +++--- components/post/PostToc.tsx | 10 +++++----- lib/hooks/useRouterLoading.ts | 16 ++++++---------- 5 files changed, 23 insertions(+), 27 deletions(-) diff --git a/components/NavBar.tsx b/components/NavBar.tsx index b2ae4bd..27041c6 100644 --- a/components/NavBar.tsx +++ b/components/NavBar.tsx @@ -3,7 +3,7 @@ import { DocSearch } from '@docsearch/react'; import cn from 'classnames'; import dynamic from 'next/dynamic'; import Link from 'next/link'; -import { useCallback, useEffect, useState } from 'react'; +import { useEffect, useState } from 'react'; import { FiMenu } from 'react-icons/fi'; const DarkModeBtn = dynamic(() => import('components/DarkModeBtn')); @@ -47,9 +47,9 @@ const parentIdChecker = (el: HTMLElement | null): boolean => { const HeadBar = () => { const [showMenu, setShowMenu] = useState(false); - const handleClick = useCallback(() => { + const handleClick = () => { setShowMenu((showMenu) => !showMenu); - }, []); + }; const [mounted, setMounted] = useState(false); useEffect(() => setMounted(true), []); @@ -57,15 +57,15 @@ const HeadBar = () => { /** * Click anywhere to close nav on mobile. */ - const handleCloseNav = useCallback((e: TouchEvent) => { + const handleCloseNav = (e: TouchEvent) => { parentIdChecker(e.target as HTMLElement) || setShowMenu(false); - }, []); + }; useEffect(() => { window.addEventListener('touchstart', handleCloseNav); return () => { window.removeEventListener('touchstart', handleCloseNav); }; - }, [handleCloseNav]); + }, []); return ( <> diff --git a/components/RUA/RUACodeSandbox.tsx b/components/RUA/RUACodeSandbox.tsx index 188b413..a4954c0 100644 --- a/components/RUA/RUACodeSandbox.tsx +++ b/components/RUA/RUACodeSandbox.tsx @@ -1,7 +1,7 @@ import classNames from 'classnames'; import useInView from 'lib/hooks/useInView'; import { useTheme } from 'next-themes'; -import { useCallback, useEffect, useRef, useState } from 'react'; +import { useEffect, useState } from 'react'; import RUALoading from './loading/RUALoading'; const partten = @@ -31,9 +31,9 @@ const RUACodeSandbox = ({ url }: Props) => { }, [currentTheme, embed, inView]); const [load, setLoad] = useState(false); - const handleLoad = useCallback(() => { + const handleLoad = () => { setLoad(true); - }, []); + }; if (!isUrl) return null; diff --git a/components/RUA/tab/index.tsx b/components/RUA/tab/index.tsx index 687a0e1..7a10210 100644 --- a/components/RUA/tab/index.tsx +++ b/components/RUA/tab/index.tsx @@ -1,5 +1,5 @@ import classNames from 'classnames'; -import React, { useCallback, useState } from 'react'; +import React, { useState } from 'react'; import { ItemProps } from './TabItem'; type Props = { @@ -9,9 +9,9 @@ type Props = { const Tab = ({ defaultValue, children }: Props) => { const [currentValue, setCurrentValue] = useState(defaultValue); - const handleSwitch = useCallback((value: ItemProps['value']) => { + const handleSwitch = (value: ItemProps['value']) => { setCurrentValue(value); - }, []); + }; // Pass current selected state to child const childrenWithProps = React.Children.map(children, (child) => { diff --git a/components/post/PostToc.tsx b/components/post/PostToc.tsx index dcb3b80..3732668 100644 --- a/components/post/PostToc.tsx +++ b/components/post/PostToc.tsx @@ -1,9 +1,9 @@ -import { getHeadings, SingleToc } from 'lib/utils'; -import Anchor from 'components/mdx/Anchor'; -import styles from './PostToc.module.css'; import classNames from 'classnames'; -import { Fragment, useCallback, useState } from 'react'; +import Anchor from 'components/mdx/Anchor'; +import { SingleToc } from 'lib/utils'; +import { Fragment, useState } from 'react'; import { FiChevronDown } from 'react-icons/fi'; +import styles from './PostToc.module.css'; interface Props { toc: SingleToc[]; @@ -22,7 +22,7 @@ const TocItem = ({ item }: { item: SingleToc }) => { const PostToc = ({ toc, tocLength }: Props) => { const [show, setShow] = useState(false); - const handleClick = useCallback(() => setShow((show) => !show), []); + const handleClick = () => setShow((show) => !show); return ( <> diff --git a/lib/hooks/useRouterLoading.ts b/lib/hooks/useRouterLoading.ts index cdcb6c2..b971cab 100644 --- a/lib/hooks/useRouterLoading.ts +++ b/lib/hooks/useRouterLoading.ts @@ -1,5 +1,5 @@ import { useRouter } from 'next/router'; -import { useState, useCallback, useEffect } from 'react'; +import { useEffect, useState } from 'react'; /** * Loading state when router changed. @@ -9,16 +9,12 @@ const useRouterLoading = () => { const router = useRouter(); const [loading, setLoading] = useState(false); - const handleStart = useCallback( - (url: string) => { - url !== router.pathname ? setLoading(true) : setLoading(false); - }, - [router.pathname] - ); - - const handleComplete = useCallback(() => { + const handleStart = (url: string) => { + url !== router.pathname ? setLoading(true) : setLoading(false); + }; + const handleComplete = () => { setLoading(false); - }, []); + }; useEffect(() => { router.events.on('routeChangeStart', handleStart);