• 카테고리

    질문 & 답변
  • 세부 분야

    풀스택

  • 해결 여부

    미해결

Encountered two children with the same key

24.04.25 16:49 작성 조회수 39

0

Multer 이용하여 이미지 업로드 하면 다음 오류가 뜹니다

 



import React from 'react'
import Dropzone from 'react-dropzone'
import axiosInstance from '../utils/axios';
import PropTypes from 'prop-types';

const FileUpload = ({ onImageChange, images }) => {

  const handleDrop = async (files) => {
    let formData = new FormData();

    const config = {
      header: { 'content-type': 'multipart/form-data' }
    }

    formData.append('file', files[0]);

    try {
      const response = await axiosInstance.post('/products/image', formData, config);
      onImageChange([...images, response.data.fileName]);

    } catch (error) {
      console.error(error);
    }

  }



  return (
    <div className='flex gap-4'>

      <Dropzone onDrop={handleDrop}>
        {({ getRootProps, getInputProps }) => (
          <section
            className='min-w-[300px] h-[300px] border flex items-center justify-center'
          >
            <div {...getRootProps()}>
              <input {...getInputProps()} />
              <p className='text-3xl'>+</p>
            </div>
          </section>
        )}
      </Dropzone>

      {images.map((image, index) => (
  <div key={image+index}> {/* image 이름과 index를 조합하여 고유한 키를 생성 */}
    <img
      className='min-w-[300px] h-[300px]'
      src={`${import.meta.env.VITE_SERVER_URL}/${image}`}
      alt={image}
    />
  </div>
))}

    </div>
  )
}

FileUpload.propTypes = {
  //  onImagesChnage: PropTypes.bool.isRequired,
  onImageChange: PropTypes.any,
  images: PropTypes.any,
};

export default FileUpload

 

2024-04-25 16 46 11.png

답변 1

답변을 작성해보세요.

0

sukasimarikan123님의 프로필

sukasimarikan123

2024.04.26

Pressing the spacebar or clicking the mouse unlocks appealing support skills in the enjoyable

spacebar clicker