简体   繁体   中英

fixing warning ///Each child in a list should have a unique "key" prop

i am having the same issue here in the below code. I have added a key where think it is appropriete but still getting key warning message. Hi guys, i am having the same issue here in the below code. I have added a key where think it is appropriete but still getting key warning message.

Can you help, please?
Leo

import { useState } from "react";
import { FaTimes } from "react-icons/fa";
import EditSkillsForm from "./editSkillsForm";
const SkillsList = ({ inputs, handleDeleteClick, handleCancelClick, handleEditClick, setIsEditing, handleUpdateInput,currentInput}) => {
const [ isEditing, setISEditing ] = useState(false);
  //const [ currentSkill, setCurrentSkill ] = useState({});

 handleEditClick = () =>  {
     setISEditing(true);
     console.log("editing", true)

  }
  

  


  return (

    <div className="col-lg-4 col-sm-6 mb-4">
      <div className="card p-2">
        <div className="card-Header text-center mb-4">
          <h4 className="card-title mt-2">Mastered Skills List</h4>
        </div>

        {inputs.map((skill, idx) => {
          return (
            <>
              <div className="card width: 10rem; mb-3 p-2" key={idx}>
                <div className="card-header text-center mb-4">
                  <h5 className="card-title mt-2">{skill}</h5>
                </div>
                <img src="../../logo.svg" className="card-img-top" alt="..." key={idx} />
                <div className="card-body">
                  <div className="card-title text-center mt-2 " key={idx}>
                    {
                      isEditing ? (
                        <div key={idx}>
                        <EditSkillsForm 
                        inputs={inputs} 
                        editing={setIsEditing} 
                        cancelClick= {handleCancelClick}
                        updateInput = {handleUpdateInput}
                        currentInput={currentInput}
                              
                         />
                        </div>
                      ) : null
                    }
                    {/* <EditSkillsForm inputs={inputs} /> */}
                    <ol key={idx}>
                      Skill name: {skill}
                    </ol>
                  </div>
                </div>
                <div className="card-footer" >
                  <div className="row mb-2" key={idx}>
                  <button
                    className="btn btn-primary w-100"
                    onClick={() => handleEditClick(inputs.idx)}>Edit
                  </button>
                  </div>
                  <div className="row">
                  <FaTimes className="mb-2" fill="#ffc800" onClick={() => handleDeleteClick(idx)} key={idx} />
                  </div>
                </div>
              </div>
            </>
          )
        }
        )}
      </div>
    </div>
  );
}

export default SkillsList;

the key must be unique for all the elements in your component. as I can see you using the same key idx in multiple div elements so it isnt unique anymore. try to make for each div a special name in addition of that idx value to make it unique for each one.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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