简体   繁体   English

Eslint 无法识别函数内部定义的 FunctionComponent 并抱怨在其中使用钩子

[英]Eslint doesn't recognize a FunctionComponent defined inside a function and complains about using a hook inside it

I have a component-creating function:我有一个组件创建功能:

import useViewportCategory from '../../../hooks/viewport/useViewportCategory'

type CreateViewportRestrictor = (
    displayName: string,
    settings: { showOnDesktop?: boolean, showOnTablet?: boolean, showOnMobile?: boolean },
) => FunctionComponent

const createViewportRestrictor: CreateViewportRestrictor = (
    displayName,
    { showOnDesktop = false, showOnTablet = false, showOnMobile = false },
) => {
    const component: FunctionComponent = ({ children }) => {
        const viewportCategory = useViewportCategory()

        if (viewportCategory === 'DESKTOP' && !showOnDesktop) return null
        if (viewportCategory === 'MOBILE'  && !showOnMobile ) return null
        if (viewportCategory === 'TABLET'  && !showOnTablet ) return null

        return <>{children}</>
    }

    component.displayName = displayName

    return component
}

That I use to generate components for my app ( MobileOnly , TabletOnly , etc.).我用来为我的应用程序生成组件( MobileOnlyTabletOnly等)。

However eslint complains about the use of the useViewportCategory hook and pretents me from running the app:然而 eslint 抱怨useViewportCategory钩子的使用,并假装我运行应用程序:

React Hook "useViewportCategory" is called in function "component: FunctionComponent" which is neither a React function component or a custom React Hook function react-hooks/rules-of-hooks React Hook "useViewportCategory" 在函数 "component: FunctionComponent" 中调用,它既不是 React 函数组件也不是自定义 React Hook 函数 react-hooks/rules-of-hooks

Interestingly, casting the component in TypeScript fixes the error:有趣的是,在 TypeScript 中转换组件修复了错误:

const component: FunctionComponent = (({ children }) => {
    const viewportCategory = useViewportCategory()

    if (viewportCategory === DESKTOP && !showOnDesktop) return null
    if (viewportCategory === MOBILE  && !showOnMobile ) return null
    if (viewportCategory === TABLET  && !showOnTablet ) return null

    return <>{children}</>
}) as FunctionComponent

What is going on here?这里发生了什么?
Why does es lint recognize function components defined directly inside module scope but not here?为什么 es lint 识别直接在模块范围内定义的函数组件而不是在这里?
What can I do to fix this error without type casting / disabling eslint rule?我该怎么做才能在不进行类型转换/禁用 eslint 规则的情况下修复此错误?

Try writing your function in PascalCase:尝试在 PascalCase 中编写您的函数:

const Component: FunctionComponent = (({ children }) => {
    const viewportCategory = useViewportCategory()

    if (viewportCategory === DESKTOP && !showOnDesktop) return null
    if (viewportCategory === MOBILE  && !showOnMobile ) return null
    if (viewportCategory === TABLET  && !showOnTablet ) return null

    return <>{children}</>
})

Lint rule expects function to be written in PascalCase. Lint 规则期望函数以 PascalCase 编写。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么 jest 不调用 useEffect 钩子内的函数? - Why jest doesn't call a function that is inside the useEffect hook? 在 function 中反应钩子 useState 的设置器不起作用 - React hook useState's setter inside function doesn't work React 本机挂钩 state 不会在 function 内更新 - React native hook state doesn't update inside a function 当我的组件被传递给另一个组件进行渲染时,React 抱怨没有在主体 function 内部使用钩子 - React complains that hook is not used inside of a body function when my component is passed into another component to render 不能在 function 中使用反应钩子 - Can't use react hook inside a function 使用 useState 钩子时,打字稿抱怨类型 - When using useState hook, typescript complains about type ESLint检查文件夹内的文件,但不检查文件夹内的文件夹。 我该如何解决? - ESLint checks files inside a folder but doesn't check the folders inside the folder. How can I fix it? useEffect 挂钩内的 Graphql 订阅无法访问最新状态 - Graphql subscriptions inside a useEffect hook doesn't access latest state 在异步函数中使用 React useState 钩子更新函数 - Using the React useState hook update function inside a Async function function 内部的无效挂钩调用 - Invalid hook call inside function
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM