How a write a function that receives a component then adds some parameter to it then returns the new component

huangapple go评论93阅读模式
英文:

How a write a function that receives a component then adds some parameter to it then returns the new component

问题

我想编写一个函数,该函数将使用 Icon 组件。我在这里使用 lucide-react。

这是我编写的函数:

const withIcon = (OriginalComponent) => {
  return function NewComponent() {
    return <OriginalComponent className='text-blue-400' />;
  };
};

我的输入组件是这样的:

import { cn } from '@/lib/utils';
import React, { HtmlHTMLAttributes } from 'react';

interface CompType {
  icon?: React.ReactNode;
}

export default function Input({
  className,
  icon,
  ...props
}: HtmlHTMLAttributes<HTMLInputElement> & CompType) {
  return (
    <div className='shadow-md hover:shadow-lg transition-all px-6 flex items-center h-14 text-4xl rounded-full overflow-hidden'>
      <input {...props} className={cn('outline-none grow', {}, className)} />
      {withIcon(icon)}
    </div>
  );
}

然后我从这里传递我的 Icon:

import Input from '@/components/atoms/input';
import { User2 } from 'lucide-react';

export default function Login() {
  return (
    <section className='container'>
      <Input placeholder='email' icon={<User2 />} />
    </section>
  );
}

它会报错,错误信息如下:"Functions are not valid as a React child. This may happen if you return a Component instead of from render. Or maybe you meant to call this function rather than return it."

如何修复这个问题,提前感谢。

英文:

I want to write a function that will take the Icon component. I am using lucide-react here.

Here I wrote the function

const withIcon = (OriginalComponent) =&gt; {
  return function NewComponent() {
    return &lt;OriginalComponent className=&#39;text-blue-400&#39; /&gt;;
  };
};

And my input component is this

import { cn } from &#39;@/lib/utils&#39;;
import React, { HtmlHTMLAttributes } from &#39;react&#39;;

interface CompType {
  icon?: React.ReactNode;
}

export default function Input({
  className,
  icon,
  ...props
}: HtmlHTMLAttributes&lt;HTMLInputElement&gt; &amp; CompType) {
  return (
    &lt;div className=&#39;shadow-md hover:shadow-lg transition-all px-6 flex items-center h-14 text-4xl rounded-full overflow-hidden&#39;&gt;
      &lt;input {...props} className={cn(&#39;outline-none grow&#39;, {}, className)} /&gt;
      {withIcon(icon)}
    &lt;/div&gt;
  );
}

And from here I am passing my Icon

import Input from &#39;@/components/atoms/input&#39;;
import { User2 } from &#39;lucide-react&#39;;

export default function Login() {
  return (
    &lt;section className=&#39;container&#39;&gt;
      &lt;Input placeholder=&#39;email&#39; icon={&lt;User2 /&gt;} /&gt;
    &lt;/section&gt;
  );
}

It gives a error like this, "Functions are not valid as a React child. This may happen if you return a Component instead of <Component /> from render. Or maybe you meant to call this function rather than return it."

How to fix this, thanks in advance.

答案1

得分: 1

只返回翻译好的部分:

我建议只需创建一个接受 Lucide FC 包装器作为属性的 React 组件。

示例:

import { LucideProps, UserPlus } from 'lucide-react';

function Icon({ icon: Icon }: { icon: React.FC<LucideProps> }) {
  return <Icon className="text-blue-400" />;
}

export default function Login() {
  return (
    <div>
      一些内容 <Icon icon={UserPlus} />
    </div>
  );
}

额外提示:我建议通过创建一个专用文件来组织您的图标导入/导出,而不是直接在 React 组件中使用它们。例如,您可以创建一个 icons.tsx 文件,在其中重新导出 Lucide 图标并附带一个可选的包装器。这种方法允许您轻松地整合来自各种库的图标或添加自己的自定义品牌图标,同时保持一致的接口。它还简化了将来更换图标的过程。

英文:

I would simply create a React Component that takes the Lucide FC wrapper as prop.

Example:

import { LucideProps, UserPlus } from &#39;lucide-react&#39;;

function Icon({ icon: Icon }: { icon: React.FC&lt;LucideProps&gt; }) {
  return &lt;Icon className=&quot;text-blue-400&quot; /&gt;;
}

export default function Login() {
  return (
    &lt;div&gt;
      Some content &lt;Icon icon={UserPlus} /&gt;
    &lt;/div&gt;
  );
}

Bonus tip: I suggest organizing your Icon import/exports by creating a dedicated file instead of directly using them in your React components. For example, you can create an icons.tsx file where you re-export Lucide Icons with an optional wrapper. This approach allows you to easily incorporate icons from various libraries or add your own custom brand icons while maintaining a consistent interface. It also simplifies the process of swapping out icons in the future.

huangapple
  • 本文由 发表于 2023年7月10日 17:57:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/76652642.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定