提问人:Rabindra Mahato 提问时间:11/17/2023 更新时间:11/17/2023 访问量:12
下一个JS |ReactJS 在我使用将道具“variant”传递给 shadCn 按钮时发出警告
NextJS | ReactJS giving warning when i am using passing props "variant" to shadCn Button
问:
类型“{ children: string; variant: string; }”不能分配给类型“IntrinsicAttributes & ButtonProps & RefAttributes”。 属性“variant”在类型“IntrinsicAttributes & ButtonProps & RefAttributes”上不存在。ts(2322)
export default function Home() {
return (
<main>
<Button variant="outline">Button</Button>
</main>
)
}
ShadCn 代码
import * as React from "react"
import { Slot } from "@radix-ui/react-slot"
import { cva, type VariantProps } from "class-variance-authority"
import { cn } from "@/lib/utils"
const buttonVariants = cva(
"inline-flex items-center justify-center whitespace-nowrap rounded-md text-sm font-medium ring-offset-background transition-colors focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2 disabled:pointer-events-none disabled:opacity-50",
{
variants: {
variant: {
default: "bg-primary text-primary-foreground hover:bg-primary/90",
destructive:
"bg-destructive text-destructive-foreground hover:bg-destructive/90",
outline:
"border border-input bg-background hover:bg-accent hover:text-accent-foreground",
secondary:
"bg-secondary text-secondary-foreground hover:bg-secondary/80",
ghost: "hover:bg-accent hover:text-accent-foreground",
link: "text-primary underline-offset-4 hover:underline",
},
size: {
default: "h-10 px-4 py-2",
sm: "h-9 rounded-md px-3",
lg: "h-11 rounded-md px-8",
icon: "h-10 w-10",
},
},
defaultVariants: {
variant: "default",
size: "default",
},
}
)
export interface ButtonProps
extends React.ButtonHTMLAttributes<HTMLButtonElement>,
VariantProps<typeof buttonVariants> {
asChild?: boolean
}
const Button = React.forwardRef<HTMLButtonElement, ButtonProps>(
({ className, variant, size, asChild = false, ...props }, ref) => {
const Comp = asChild ? Slot : "button"
return (
<Comp
className={cn(buttonVariants({ variant, size, className }))}
ref={ref}
{...props}
/>
)
}
)
Button.displayName = "Button"
export { Button, buttonVariants }
它在 nextjs 的每个组件上都出现错误,例如,当我导入 Next/image 时,它给了我警告
答:
0赞
Rabindra Mahato
11/17/2023
#1
我解决了它,它是由tsconfig.json文件引起的
将 “moduleResolution”: “bundler”, 更改为 “moduleResolution”: “Node”,解决我的问题
评论