英文:
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
如何修复这个问题,提前感谢。
英文:
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) => {
return function NewComponent() {
return <OriginalComponent className='text-blue-400' />;
};
};
And my input component is this
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>
);
}
And from here I am passing my 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>
);
}
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 'lucide-react';
function Icon({ icon: Icon }: { icon: React.FC<LucideProps> }) {
return <Icon className="text-blue-400" />;
}
export default function Login() {
return (
<div>
Some content <Icon icon={UserPlus} />
</div>
);
}
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论