简体   繁体   English

错误:有条件地调用 React Hook “useCallback”。 在每个组件渲染中,必须以完全相同的顺序调用 React Hooks

[英]Error: React Hook "useCallback" is called conditionally. React Hooks must be called in the exact same order in every component render

Getting a build error usecallback hook called conditinally.获取构建错误使用有条件调用的回调钩子。 if i dont use useCallback i get the error JSX props should not use arrow functions如果我不使用 useCallback 我得到错误 JSX props should not use arrow functions
const LinkComp = <T extends {}>(props: LinkProps & T extends AnchorProps? AnchorProps: ButtonProps) => { const { title, hideTitle, children, url = '', action, label, newWindow, className, iconName, isExternal, inheritColor = true, underlineOnHover = true, underline = false, theme = '', bold = false, onClick, modal = false, forceAnchorTag = false, appendQueryParams = true, showOutline = true, ...linkProps } = props; const LinkComp = <T extends {}>(props: LinkProps & T extends AnchorProps? AnchorProps: ButtonProps) => { const { title, hideTitle, children, url = '', action, label, newWindow, class, isName inheritColor = true, underlineOnHover = true, underline = false, theme = '', bold = false, onClick, modal = false, forceAnchorTag = false, appendQueryParams = true, showOutline = true, ...linkProps } = props; const [handleClick] = useRouter(action, url); const [handleClick] = useRouter(action, url); const forwardedParams = useSelector(selectForwardedQueryParams);常量 forwardedParams = useSelector(selectForwardedQueryParams);

const linkContent = (
    <>
        {iconName && <Icon name={iconName} className='mr-3' />}
        {!hideTitle && (title || children)}
    </>
);

if (modal) {
    if (linkProps.modalTitle) delete linkProps.modalTitle;

    return (
        <button {...(anchorProps as ButtonProps)} role='link' onClick={onClick as ButtonProps['onClick']}>
            {linkContent}
        </button>
    );
}

// queryString.stringifyUrl combines the params from both url and forwarded params.
// don't append the params for `tel` links
const forwardedParamsUrl = queryString.stringifyUrl({ url, query: !url?.includes('tel:') && forwardedParams });

const handleAnchorClick = useCallback((e: React.MouseEvent<HTMLAnchorElement, MouseEvent>) =>  {
    if (handleClick) handleClick(e);
    
    return true;
},[handleClick]);

if (forceAnchorTag) {
    return (
        <a href={forwardedParamsUrl} {...(anchorProps as AnchorProps)} onClick={handleAnchorClick}>
            {linkContent}
        </a>
    );
}
// search extras uses the query params in a different way, so no need to append them here
const fullUrl = appendQueryParams ? forwardedParamsUrl : url;

return (
    <Link href={fullUrl} passHref={isExternal || newWindow}>
        <a {...(anchorProps as AnchorProps)} onClick={handleClick}>
            {linkContent}
        </a>
    </Link>
);

}; };

export default LinkComp;导出默认 LinkComp;

You shouldn't be passing a function into a useCallback hook.您不应该将 function 传递给 useCallback 挂钩。 Why are you doing that?你为什么这样做?

暂无
暂无

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

相关问题 有条件地调用 React Hook “useCustomHook”。 在每个组件渲染中,必须以完全相同的顺序调用 React Hooks - React Hook "useCustomHook" is called conditionally. React Hooks must be called in the exact same order in every component render React Hook“useEffect”被有条件地调用。 React Hooks 必须在每个组件渲染中以完全相同的顺序调用 - React Hook "useEffect" is called conditionally. React Hooks must be called in the exact same order in every component render React Hook "useState" 被有条件地调用。 在每个组件渲染错误中,必须以完全相同的顺序调用 React Hooks - React Hook "useState" is called conditionally. React Hooks must be called in the exact same order in every component render error useNameProps" 被有条件地调用。React Hooks 必须在每个组件渲染中以完全相同的顺序调用 - useNameProps" is called conditionally. React Hooks must be called in the exact same order in every component render ReactJS ESlint 钩子错误 React Hooks 必须在每个组件渲染中以完全相同的顺序调用 - ReactJS ESlint hook error React Hooks must be called in the exact same order in every component render 在构建应用程序时,必须在每个组件呈现错误中以完全相同的顺序调用 React Hooks - NextJs/Javascript - React Hooks must be called in the exact same order in every component render error when building app - NextJs/Javascript 必须以完全相同的顺序调用 React Hook 但我遇到了异常 - React Hook must be called in the exact same order BUT I got an exception React Hook“useState”无法在 class 组件中调用。 React Hooks 必须在 React function 组件或自定义 React Hook function 中调用 - React Hook "useState" cannot be called in a class component. React Hooks must be called in a React function component or a custom React Hook function 有条件地调用 React Hook “useEffect” - React Hook "useEffect" is called conditionally 即使它违反了钩子的规则,useCallback React 钩子也可以有条件地使用吗? - Can the useCallback React hook be used conditionally even if it breaks the rules of hooks?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM