๋งํฌ์„œ๋น„์Šค ๋“ฑ๋กํ™”๋ฉด์—์„œ Input Value์„ ๊ฐ€์ ธ์˜ค๋Š” ๋ฐฉ๋ฒ•์„ ์•Œ์•˜๋‹ค.

๊ทธ๋Ÿผ ์ด๋ฏธ์ง€ ๋“ฑ๋ก์„ ์œ„ํ•œ Drag&Drop ์ด๋ฏธ์ง€ ์—…๋กœ๋“œ๋ฅผ ์ œ์ž‘ํ•ด๋ณด์ž.

 

#11. Reactํ™”๋ฉด์—์„œ ๊ฐ ํ•ญ๋ชฉ์˜ Input Value์„ ๊ฐ€์ ธ์˜ค๊ธฐ

 

#11. Reactํ™”๋ฉด์—์„œ ๊ฐ ํ•ญ๋ชฉ์˜ Input Value์„ ๊ฐ€์ ธ์˜ค๊ธฐ

#10. React ๋งํฌ์„œ๋น„์Šค ๋“ฑ๋ก ๋ฐ ํŽธ์ง‘ ํ™”๋ฉด ๊ฐœ๋ฐœ(๋“ฑ๋กํ™”๋ฉด ๋””์ž์ธ) #10. React ๋งํฌ์„œ๋น„์Šค ๋“ฑ๋ก ๋ฐ ํŽธ์ง‘ ํ™”๋ฉด ๊ฐœ๋ฐœ(๋“ฑ๋กํ™”๋ฉด ๋””์ž์ธ) #9. ์ดˆํ™”๋ฉด ๋””์ž์ธ ( CSS ์ ์šฉ ๋ฐ ํ™”๋ฉด Component์ œ์ž‘ ) #9. ์ดˆํ™”๋ฉด ๋””์ž์ธ

firstvalue.tistory.com

 

โŽŸโŽœ์ด๋ฏธ์ง€Drag Drop๊ธฐ๋Šฅ ๋ฐ Preview ๊ธฐ๋Šฅ

 

โŒ˜ links.create.jsx ( ์œ„์น˜: linkservice/client/src/component/links.create.jsx)

---------------------------------------------------------------------------------------------------

1)   ์ด๋ฏธ์ง€๋ฅผ ๋ฏธ๋ฆฌ ๋ณผ ์ˆ˜ ์žˆ๋Š” ๊ธฐ๋Šฅ

     preview const ์„ ์–ธ

const [preview, setPreview] = useState('assets/default.jpg')

๐Ÿ“Œ ์ด๋ฏธ์ง€๊ฐ€ ์—†์„ ๋•Œ default์ด๋ฏธ์ง€๋ฅผ ๋ณด์—ฌ ์ฃผ๊ธฐ ์œ„ํ•ด์„œ public/assets/default.jpg์„ ๋„ฃ์–ด๋‘”๋‹ค.

( ์ƒ˜ํ”Œ ์ด๋ฏธ์ง€๋Š” ์ธํ„ฐ๋„ท ๊ฒ€์ƒ‰์œผ๋กœ ์ ๋‹นํ•œ ๊ณณ์—์„œ ๊ฐ€์ ธ์˜ค๋ฉด ๋œ๋‹ค.)

 

   img src ์— preview const์„ ์ง€์ •

<div className="img_wrap" > 
            <img src =  {preview}
                alt='์ด๋ฏธ์ง€' className="img_box" />
            <input multiple={false} name='imageurl'/>
 </div>

   handleChange() ํ•จ์ˆ˜์— target name์ด imageurl์ผ ๊ฒฝ์šฐ setPreview๊ฐ’์„ ์…‹ํŒ… ํ•œ๋‹ค.

const handleChange = (e) => {
        const { name, value } = e.target
        setValues((prevValues) => ({
            ...prevValues,
            [name]: value,
        }))
        
       // ์ด ๋ถ€๋ถ„์„ ์ถ”๊ฐ€
        if (e.target.name === 'imageurl') {
            setPreview(e.target.value)
        }
    }

2)   ์ด๋ฏธ์ง€ Drag&Drop๊ธฐ๋Šฅ ๊ตฌํ˜„

Drag Drop๊ธฐ๋Šฅ์€ React์—์„œ ์ œ๊ณต๋˜๋Š” react-dropzone ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค.

๐Ÿ“Œ ๊ณต์‹๋ฌธ์„œ : https://github.com/react-dropzone/react-dropzone

 

ํ„ฐ๋ฏธ๋„์—์„œ yarn add react-dropzone ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์„ค์น˜ํ•œ ๋‹ค์Œ,

useDropzone์„ ์„ ์–ธํ•œ๋‹ค.

import {useDropzone} from 'react-dropzone'

image state์„ ์„ ์–ธํ•œ๋‹ค.

const [image, setImage] = useState(null)

onDrop ๊ด€๋ จ ํ•จ์ˆ˜๋ฅผ ์ž‘์„ฑํ•œ๋‹ค.

onDropํ•จ์ˆ˜๋Š” ์ด๋ฏธ์ง€ dropํ›„ ์ฒ˜๋ฆฌํ•ด์•ผ ํ•˜๋Š” ์ž‘์—…๋‚ด์šฉ์„ ๋ฐ˜์˜ํ•˜๋ฉด ๋œ๋‹ค.

์—ฌ๊ธฐ์„œ๋Š” ํŒŒ์ผ์„ ์ฝ์–ด ์ฒ˜๋ฆฌ๋Š” ๋กœ์ง์„ ๋ฐ˜์˜ ํ•œ๋‹ค.

function onDrop(acceptedFiles) {
        const reader = new FileReader();
        const file = acceptedFiles
        
		// ์ด๋ฏธ์ง€ ํŒŒ์ผ์„ ์ฝ์–ด setImage ๋กœ ์ €์žฅ
        if (file) {
          reader.readAsDataURL(file[0]);
          setImage(file[0]);
        }
       // onDrop๋˜๋ฉด preview ๋˜๊ฒŒ ์ฒ˜๋ฆฌํ•˜๊ณ , ๊ธฐ์กด ์ด๋ฏธ์ง€ url์ •๋ณด๋ฅผ ๊ณต๋ฐฑ์ฒ˜๋ฆฌํ•œ๋‹ค.
        reader.onload = (e) => {
            setPreview(reader.result);
            document.getElementsByName("imageurl")[0].value = ''
        };
    }

readAsDataURL ๋ฉ”์†Œ๋“œ๋กœ imageํŒŒ์ผ์„ base64๋กœ ์ธ์ฝ”๋”ฉ ๋œ ๊ฐ’์œผ๋กœ  ๋ฐ˜ํ™˜ํ•œ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ๊ทธ ๊ฒฐ๊ณผ๋ฅผ setPreview(๋ฏธ๋ฆฌ๋ณด๊ธฐ) ์— ๊ฐ’์„ ๋„ฃ๊ณ , setImage์€ ์ด๋ฏธ์ง€ ํŒŒ์ผ์ •๋ณด๋ฅผ ๋ณด๊ด€ํ•œ๋‹ค. ๋งˆ์ง€๋ง‰์œผ๋กœ ์ด๋ฏธ์ง€๋ฅผ ์ง์ ‘ ๋“ฑ๋กํ–ˆ์œผ๋ฏ€๋กœ,

๋งํฌ์ •๋ณด URL(imageurl) ํ•ญ๋ชฉ์„ ๋น„์šด๋‹ค.

 

Dropzone์˜ Props์ธ getRootProps์™€ getInputProps์ง€์ •ํ•œ๋‹ค.

const {getRootProps, getInputProps} = useDropzone({onDrop})

์ด๋ฏธ์ง€ ์˜์—ญ(zone)์˜ div์— props๋“ค์„ ์„ ์–ธํ•œ๋‹ค.

<div className="img_wrap" {...getRootProps()} > 
            <img src =  {preview}
                alt='์ด๋ฏธ์ง€' className="img_box" />
            <input {...getInputProps()} multiple={false} name='imageurl'/>
</div>

๐Ÿ“Œ(์ค‘์š”) getRootProps๋Š” ์ด๋ฏธ์ง€ ์˜์—ญ์˜ div์— ,    getInputProps๋Š” input์— ์„ ์–ธํ•˜๋ฉด ๋œ๋‹ค.

์ด๋ฏธ์ง€ ์ €์žฅํ•˜๊ธฐ ์œ„ํ•ด์„œ handleSubmit์— ๊ด€๋ จ ๋‚ด์šฉ์„ ์ถ”๊ฐ€ํ•œ๋‹ค.

์ด๋ฏธ์ง€๊ฐ€ ์žˆ์„ ๊ฒฝ์šฐ formDataํ˜•ํƒœ๋กœ ์—…๋กœ๋“œ๋ฅผ ์ง„ํ–‰ํ•œ๋‹ค.

linkService.createLink(values).then(
            (res) => {
                if (image) {
                    //์ด๋ฏธ์ง€ ํ•ญ๋ชฉ์ด ์žˆ์„๊ฒฝ์šฐ formData์— ๋„ฃ์–ด API์„ ํ˜ธ์ถœํ•œ๋‹ค.
                    formData.append('image', image)
                    linkService.uploadImage(formData).then(
                        (res)=> {// console.log(res) 
                        }
                )} 
                alert(res.data.message)
                window.location.reload();            
            }
        )
์ด๋ฏธ์ง€ Drag&Drop

 

๋‹ค์ŒํŽธ์—๋Š” ์‹ค์ œ DB ์™€ ์ด๋ฏธ์ง€ ํŒŒ์ผ ์ €์žฅ (๋“ฑ๋ก) ๊ธฐ๋Šฅ์„ ๋งŒ๋“ค์–ด ๋ณด์ž.

+ Recent posts