reactjs - 使用 TypeScript 在 Nextjs 中映射数组

标签 reactjs next.js

我正在尝试以简单的方式映射数组 oj 对象


const Folders: FC = () => {
  return (
    <section className="body-center shadow-md rounded-sm flex py-4 gap-[14px] flex-wrap my-5 relative z-[1] bg-gradien-catalog">
    <div>
       {itemCatalog.map((item)=>{
        <SmallFolder name={item.name}/>
      })}
    </div>
     
 
    </section>
  );
};

export default Folders

但是我遇到了错误,我不知道问题是什么

Type 'void[]' is not assignable to type 'ReactNode'.
  Type 'void[]' is not assignable to type 'ReactFragment'.
    The types returned by '[Symbol.iterator]().next(...)' are incompatible between these types.
      Type 'IteratorResult<void, any>' is not assignable to type 'IteratorResult<ReactNode, any>'.
        Type 'IteratorYieldResult<void>' is not assignable to type 'IteratorResult<ReactNode, any>'.
          Type 'IteratorYieldResult<void>' is not assignable to type 'IteratorYieldResult<ReactNode>'.
            Type 'void' is not assignable to type 'ReactNode'.ts(2322)
index.d.ts(1376, 9): The expected type comes from property 'children' which is declared here on type 'DetailedHTMLProps<HTMLAttributes<HTMLDivElement>, HTMLDivElement>'

这是我的映射组件

interface iFolder{
  name: string
}

const SmallFolder: FC<iFolder> = ({name}) => {
  return (
    <Link
      href="/catalog"
      className="folder-container-style group grow-0 shrink flex-[278px]"
      style={{
        backgroundImage: `url("https://img.freepik.com/free-photo/flat-lay-health-still-life-arrangement-with-copy-space_23-2148854064.jpg?w=2000")`,
      }}
    >
      <p className="folder-title">{name}</p>
  
      <figcaption
        className="absolute left-0 bottom-0 flex items-end justify-center pb-5 bg-[#16406a6b] h-[40%] w-full translate-y-full
                             group-hover:translate-y-0 transition-transform"
      ></figcaption>
    </Link>
  );
};

export default SmallFolder;

我尝试在 div 之外添加 React 片段

   <>
       {itemCatalog.map((item)=>{
        <SmallFolder name={item.name}/>
      })}
    </>

错误消失了,但页面上没有任何更改。

这是我的数据

import { TypesItem } from "@/Types/common-types";

export const itemCatalog: TypesItem[] = [
  {
    name: "Ортопедия",
    link: "orthopedics",
  },
  {
    name: "Аппликаторы и массажные коврики",
    link: "matsAndAplicators ",
  },
  {
    name: "Расспродажа",
    link: "sales ",
  },
];

我不知道问题是什么,当我尝试直接使用组件而不映射它时,它工作得很好。 猜猜问题是什么?**

最佳答案

您返回的是 array<void>而不是array<FC<iFolder>> 。您可以添加 return :

{itemCatalog.map((item)=> {
  return <SmallFolder name={item.name} />
})}

或删除大括号 {...} :

{itemCatalog.map((item)=> (<SmallFolder name={item.name} />)}

关于reactjs - 使用 TypeScript 在 Nextjs 中映射数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/75723588/

相关文章:

node.js - 具有不同路径的代理下的 Next.js

javascript - useRef() 无效 Hook 调用 [NextJs]

next.js - Tailwinds + Ant 设计 : Button color is white but has own color wnen I hover it

javascript - 如何在 React-hooks 中创建计时器

javascript - React 组件无法读取未定义的属性

javascript - Object.keys() 返回在 Ant Design 表列渲染中不起作用

node.js - @azure/storage-blob如何从Azuree获取uploadStream图像url的结果

javascript - 如何在react-redux中返回组合的reducer?

javascript - npm 错误!该操作被您的操作系统拒绝

node.js - 如何在 useEffect() Hook 中使用异步方法