简体   繁体   English

迭代器中缺少元素的“键”道具

[英]Missing "key" prop for element in iterator

import React from 'react'
import { motion } from "framer-motion"

type Props = {}

export default function Projects({}: Props) {
    const projects = [1,2];
  return (
    <motion.div  
      initial={{ opacity: 0 }}
      whileInView={{ opacity: 1 }}
      transition={{ duration: 1.5 }}
      className='h-screen relative flex overflow-hidden flex-col text-left md:flex-row max-w-    full justify-evenly mx-auto items-center z-0'>
        <h3 className='absolute top-24 uppercase tracking-[20px] text-gray-500 text-2xl'>
            Proyectos
        </h3>

        <div className='relative w-full flex overflow-x-scroll overflow-y-hidden snap-x snap-mandatory z-20 scrollbar-thin scrollbar-track-gray-400/20 scrollbar-thumb-[#61ff45]/80'>
            {projects.map((project, i) => (
            <div className='w-screen flex-shrink-0 snap-center flex flex-col space-y-5 items-center justify-center p-20 md:p-44 h-screen'>
                <motion.img 
                    initial={{
                        y: -300,
                        opacity: 0
                      }}
                      transition={{ duration: 1.2 }}
                      whileInView={{ opacity: 1, y: 0 }}
                      viewport={{ once: true }}
                      src="./img/PHP.png" 
                      alt="" 
                />
            
                <div className='space-y-10 px-0 md:px-10 max-w-6xl'>
                    <h4 className='text-4xl font-semibold text-center'>
                        <span className='underline decoration-[#61ff45]/50'> Caso de estudio {i + 1} de {projects.length}:</span>  Mini Php Lexer
                    </h4>
                    <div className='text-lg text-center md:text-left'>
                        Este es un analizador lexico el cual tiene como proposito procesar y evaluar un codigo en PHP y decir si este se puede ejecutar sin ningun problema, mostrando en pantalla el codigo completo y parte por parte lo que es cada cosa del codigo
                    </div>
                </div>
            </div>
            ))}
            </div>

        <div className='w-full absolute top-[30%] bg-[#61ff45]/10 left-0 h-[500px] -skew-y-12'/>
    </motion.div>
  )
}

Im trying to do a portfolio but when I code npm run dev, its show me "Hydration failed because the initial UI does not match what was rendered on the server."我正在尝试做一个投资组合,但是当我编码 npm 运行 dev 时,它向我显示“Hydration 失败,因为初始 UI 与服务器上呈现的内容不匹配。” I think that the error is in this piece of code because in vscode appear that "Missing "key" prop for element in iterator".我认为错误出在这段代码中,因为在 vscode 中出现“迭代器中元素缺少“键”道具”。 I dont really know a lot about these programming languages so I dont know what that error means.我不太了解这些编程语言,所以我不知道该错误是什么意思。

write a key when using a map iterator使用 map 迭代器时写入密钥

{projects.map((project, i) => (
            <div key={i} className='w-screen flex-shrink-0 snap-center flex flex-col space-y-5 items-center justify-center p-20 md:p-44 h-screen'>
                <motion.img ...

Whenever you put any loop for rendering.每当您放置任何循环进行渲染时。 React requires key in the outer class. React 需要外部 class 中的密钥。

In your case, you should add key like this.在您的情况下,您应该像这样添加密钥。

{projects.map((project, i) => (
        <div key={project.id} className='w-screen flex-shrink-0 snap-center flex flex-col space-y-5 items-center justify-center p-20 md:p-44 h-screen'>

Please do not add index as a key as React official document mentioned here https://reactjs.org/docs/lists-and-keys.html请不要将索引添加为键,因为这里提到了 React 官方文档https://reactjs.org/docs/lists-and-keys.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM