Skip to content

NewsSite-Next workload is generating keys for <li> element on the fly #422

Closed
@lingyuncai

Description

@lingyuncai

Since the data doesn't contain an id field for each item, the workload is now generating keys for each <li> element dynamically while rendering, using uuidv4().

{content.map((item) =>
<li key={uuidv4()} className={styles["article-list-item"]}>
{item.url && !item.title
? <a href={item.url}>
<ArticleText text={item.content} />
</a>
: <ArticleText text={item.content} />
}
</li>
)}

This will cause keys to never match up between renders, leading to these elements being recreated every time, which defeats the purpose of keys and slows the performance [1].

Besides, this implementation seems inconsistent with that of NewsSite-Nuxt, which uses item.id (which is undefined) as the key binding directly.

<li v-for="item in content" :key="item.id" :class="styles['article-list-item']">
<a v-if="item.url && !item.title" :href="item.url">
<ArticleText :text="item.content" />
</a>
<ArticleText v-else :text="item.content" />
</li>

[1] https://react.dev/learn/rendering-lists#rules-of-keys

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions