Nextjs和React初学者在这里。
我为我的Nextjs网站制作了一个<ImageSlider />
组件,但由于类型不匹配,它不会呈现在我的主页上。Nextjs需要StaticImageData
,但却接收到一个string
URL。
需要注意的是我使用create-vite/react
创建了<ImageSlider />
组件following a tutorial。
以下是我在page.tsx
文件中尝试调用我的主页中的<ImageSliderApp />
时收到的错误:
'ImageSliderApp' cannot be used as a JSX component.
Its type '() => void' is not a valid JSX element type.
Type '() => void' is not assignable to type '(props: any, deprecatedLegacyContext?: any) => ReactNode'.
Type 'void' is not assignable to type 'ReactNode'.ts(2786)
字符串<ImageSliderApp />
组件引用了我的image-sliderapp.tsx
文件中的实际<ImageSlider />
,该文件将被导入到我的page.tsx
文件中:
import Image, { StaticImageData } from "next/image";
import React, { Component } from "react";
import { ImageSlider } from "./image-slider";
import WaefMvp from "../../imgsandbox/components/public/waef-mvp.png";
import WaefLbte from "../../imgsandbox/components/public/waef-lbte.png";
import WaefDnb from "../../imgsandbox/components/public/waef-dnb.png";
const imageArray = [
{ src: WaefMvp, alt: "MVP", },
{ src: WaefLbte, alt: "Lbte", },
{ src: WaefDnb, alt: "Dnb" },
];
export default function ImageSliderApp() {
return;
<div>
<ImageSlider images={imageArray} />
</div>;
}
型
但是,当我尝试在<ImageSlider />
组件中传递images={imageArray}
属性时,收到以下类型不匹配错误:
Type '{ url: string; alt: string; }[]' is not assignable to type '{ url: StaticImageData; alt: string; }[]'.
Type '{ url: string; alt: string; }' is not assignable to type '{ url: StaticImageData; alt: string; }'.
Types of property 'url' are incompatible.
Type 'string' is not assignable to type 'StaticImageData'.ts(2322)
image-slider.tsx(7, 3): The expected type comes from property 'images' which is declared here on type 'IntrinsicAttributes & ImageSliderProps'
型
最后,下面是实际组件的代码:
import Image, { StaticImageData } from "next/image";
import React, { useState } from "react";
import { ChevronLeft, ChevronRight, Dot, Asterisk } from "lucide-react";
import "./image-slider.css";
type ImageSliderProps = {
images: {
url: StaticImageData;
alt: string;
}[];
};
export function ImageSlider({ images }: ImageSliderProps) {
const [imageIndex, setImageIndex] = useState(0);
function showPrevImage() {
setImageIndex((index) => {
if (index === 0) return images.length - 1;
return index - 1;
});
}
function showNextImage() {
setImageIndex((index) => {
if (index === images.length - 1) return 0;
return index + 1;
});
}
return (
<section
aria-label="Image Carousel"
style={{ width: "100%", height: "100%", position: "relative" }}
>
<div
style={{
width: "100%",
height: "100%",
display: "flex",
overflow: "hidden",
}}
>
{images.map(({ url, alt }, index) => (
<Image
key={index}
src={url}
alt={alt}
aria-hidden={imageIndex !== index}
className="img-slider-img"
style={{ translate: `${-100 * imageIndex}%` }}
/>
))}
</div>
<button
onClick={showPrevImage}
className="img-slider-btn"
style={{ left: 0 }}
aria-label="View Previous Image"
>
<ChevronLeft aria-hidden />
</button>
<button
onClick={showNextImage}
className="img-slider-btn"
style={{ right: 0 }}
aria-label="View Next Image"
>
<ChevronRight aria-hidden />
</button>
<div
style={{
position: "fixed",
bottom: ".5rem",
left: "50%",
translate: "-50%",
display: "flex",
gap: ".25rem",
}}
>
{images.map((_, index) => (
<button
key={index}
className="img-slider-dot-btn"
onClick={() => setImageIndex(index)}
aria-label={`View Image ${index}`}
>
{index === imageIndex ? (
<Asterisk aria-hidden />
) : (
<Dot aria-hidden />
)}
</button>
))}
</div>
</section>
);
}
型
最终,Nextjs需要的是StaticImageData
,但接收到的却是string
URL。
我尝试了以下方法:
- 确保我的映像文件导入和文件路径正确
- 确保正确进口组件
- 确保类型
ImageSliderProps
接收的图像为StaticImageData
接下来我可以尝试什么?
1条答案
按热度按时间g52tjvyc1#
主要错误是“Type 'void' is not assignable to type 'ReactNode'”,因为您在ImageSliderApp组件中返回了void。
字符串
请更新如下。
型