列表中的每个子项都应该有一个唯一的“键”。支柱。检查渲染方法

发布于 2025-01-18 04:23:16 字数 1051 浏览 0 评论 0原文

你好,我对编程还很陌生,但我遇到了一个问题。我已经构建了一个带有对象数组和属性“id”的接口。我使用它(id:属性)作为我的唯一密钥,但它给了我一个错误。 这就是界面

export interface IState {
  meetupsy: {
      id: number
  }[]
}

,这是一个列表组件,

import classes from './MeetupList.module.css'
import { IState as Props } from '../../App'
import Card from '../ui/Card'

interface IProps {
    meetups: Props['meetupsy']
}

const MeetupList: React.FC<IProps> = ({meetups}) => {

    const renderList = ():JSX.Element[] => {
        return meetups.map((meetup) => {
            return <Card>
                <li key={meetup.id} className={classes.list}>
                    <div className={classes.image} />
                </li>
            </Card>
        })
    }

    return (
        <ul className={classes.render}>
            {renderList()}
        </ul>
    )
}

export default MeetupList;

我真的很困惑它说“检查 MeetupList 的渲染方法”

Hi so I am pretty new to programming and I got kinda stuck with a problem. I've built an interface with array of objects and with property 'id'. I used it (id: property) as my Unique key but it gives me an error.
Thats's the interface

export interface IState {
  meetupsy: {
      id: number
  }[]
}

and that's a List component

import classes from './MeetupList.module.css'
import { IState as Props } from '../../App'
import Card from '../ui/Card'

interface IProps {
    meetups: Props['meetupsy']
}

const MeetupList: React.FC<IProps> = ({meetups}) => {

    const renderList = ():JSX.Element[] => {
        return meetups.map((meetup) => {
            return <Card>
                <li key={meetup.id} className={classes.list}>
                    <div className={classes.image} />
                </li>
            </Card>
        })
    }

    return (
        <ul className={classes.render}>
            {renderList()}
        </ul>
    )
}

export default MeetupList;

I am really puzzled it says "Check the render method of MeetupList"

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

樱花坊 2025-01-25 04:23:16

此问题已回答多次。

您要迭代的每个元素都必须有一个 key 属性,该属性必须放置在最外层元素中并且必须是唯一的。

const renderList = (): JSX.Element[] =>
  meetups.map((meetup) => (
    <Card key={meetup.id}>
      ...
    </Card>
  ));

This question was answered multiple times.

Each of the elements you are iterating through must have a key property that must be put in the outermost element and must be unique.

const renderList = (): JSX.Element[] =>
  meetups.map((meetup) => (
    <Card key={meetup.id}>
      ...
    </Card>
  ));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文