简体   繁体   English

React 中的迭代器 react/jsx-key 中缺少元素的“key”属性

[英]Missing “key” prop for element in iterator react/jsx-key in React

I am new to React, and I'm trying to clear input when i press <button onClick={addComment} className='add-btn'>+</button> Missing "key" prop for element in iterator react/jsx-key我是 React 的新手,当我按下<button onClick={addComment} className='add-btn'>+</button>时,我试图清除输入钥匙

import React, { useContext, useState } from 'react';
import ContentHeader from './PatientHeader';
import { PatientContext } from '../App';
const moment = require('moment');

const ContentInfo = () => {
const { selectedPatient, comments, setComments } = useContext(PatientContext);
const [comment, setComment] = useState('');

const commentInput = React.createRef();

const addComment = (e: any) => {
    const date = moment();
    setComments([...comments, { comment: commentInput.current.value, date: date }]);
    e.preventDefault();
    setComment('');  
};

const handleChange = (e: any) => setComment(e.target.value);

return (
    <div className='content'>

        <ContentHeader />

        <div className='info'>
            <div className='comments'>
                <div>
                    <p>
                        <h3 className='comments-text'>Comments:</h3>
                    </p>
                    <ul>
                        {comments.map(c =>                               
                            <li>
                                <div className='new-comment'>
                                    <div>
                                        <strong>{moment(c.date).format('ll')}</strong>
                                    </div>
                                    <div>
                                        {c.comment}
                                    </div>
                                </div>
                            </li>     
                        )}
                    </ul>
                </div>

                <div className='create-commentInput'>
                    <input value={comment} ref={commentInput} onChange={handleChange} 
                     className='form-control' type="text"/>
                    <button onClick={addComment} className='add-btn'>+</button>
                </div>

            </div>  
                         
        </div>
    </div>
);
}

The following line is throwing the error, and I cannot figure out why:以下行抛出错误,我不知道为什么:

(e: any) (e: 任何)

const addComment = (e: any) => {
    const date = moment();
    setComments([...comments, { comment: commentInput.current.value, date: date }]);
    e.preventDefault();
    setComment('');  
};

How can I prevent this error from appearing?如何防止出现此错误?

It looks like your post is mostly code;看起来您的帖子主要是代码; please add some more details.请添加更多详细信息。 Thanks a lot Stackoverflow i tried but i cant非常感谢 Stackoverflow 我试过了,但我不能

When you use map to render list of components you must provide a unique key prop to rendered component - in your case you must provide key prop to <li> .当您使用map渲染组件列表时,您必须为渲染的组件提供唯一的key prop - 在您的情况下,您必须向<li>提供 key prop。

{comments.map(c =>                               
  <li key={c.id}>
    <div className="new-comment">
      <div>
        <strong>{moment(c.date).format('ll')}</strong>
      </div>
      <div>
        {c.comment}
      </div>
    </div>
  </li>
)}

Keep in mind your key prop must be unique within your data, if for some reason it is impossible for you, you can use index from Array.map method, but it's not recommended请记住,您的关键道具在您的数据中必须是唯一的,如果由于某种原因您无法实现,您可以使用Array.map方法中的index ,但不建议这样做

In order for react to be faster you should not avoid this warning and put the key prop to a tag (in this case it's li).为了使反应更快,您不应该避免此警告并将关键道具放在标签上(在本例中为 li)。 Key prop should be either an index or the id of the object, keys should not have duplicates. Key prop 应该是对象的索引或 id,键不应该有重复项。

You can resolve this with two different approaches:您可以使用两种不同的方法解决此问题:

{comments.map(c =>                               
  <li key={c.id}>
    <div className="new-comment">
      <div>
        <strong>{moment(c.date).format('ll')}</strong>
      </div>
      <div>
        {c.comment}
      </div>
    </div>
  </li>
)}

or accessing the index或访问索引

{comments.map((c, index) =>                               
  <li key={index}>
    <div className="new-comment">
      <div>
        <strong>{moment(c.date).format('ll')}</strong>
      </div>
      <div>
        {c.comment}
      </div>
    </div>
  </li>
)}

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

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