
ํธ์์๋ text๋ง ์๋๊ฒ ์๋๋ผ ์ด๋ฏธ์ง/๋์์ ๋ฑ๋ ์๋ค.
์ด๋ฅผ ๊ตฌํํ๊ธฐ ์ํด์๋ Firebase์ Storage๋ฅผ ์ฌ์ฉํ๋ฉด ๋๋ค.

bucket์ ๋ด ํ์ผ์ ๋ฃ๋ ๊ณณ์ด๋ค.
ํธ์ํ ๋ ์ฌ์ง์ ์ฒจ๋ถํ ์ ์๋๋ก type์ด file์ด๊ณ ์ด๋ฏธ์ง ํ์ผ๋ง ์ฒจ๋ถ๊ฐ๋ฅํ input์ ๋ง๋ ๋ค.
<div>
<form onSubmit={onSubmit}>
<input
type="text"
placeholder="๋ฌด์จ ์ผ์ด ์ผ์ด๋๊ณ ์๋์?"
maxLength={120}
value={tweet}
onChange={onChange}
/>
<input type="file" accept="image/*" />
<input type="submit" value="ํธ์ํ๊ธฐ" />
</form>
</div>
ํธ์ํ ๋ ์ด๋ฏธ์ง ํ์ผ์ ์ฒจ๋ถํ๋ ค๊ณ ์ ํํ๋ฉด ๋ฏธ๋ฆฌ๋ณด๊ธฐ๋ฅผ ์ ๊ณตํ์.
<input type="file" accept="image/*" onChange={onFileChange} />
event๋ฅผ ์ฝ์์ ์ฐ์ด๋ณด์.const onFileChange = (event) => {
console.log(event);
};event.target.files์์ ์ ํํ ํ์ผ์ ํ์ธํ ์ ์๋ค!

ํ์ผ๋ฆฌ์คํธ์์ ์ฒซ๋ฒ์งธ ์๋ฆฌ๋จผํธ๋ฅผ ๊ฐ์ ธ์ค์.
//file ๋ฏธ๋ฆฌ๋ณด๊ธฐ ์ ๊ณต
const onFileChange = (event) => {
//console.log(event);
const {
target: { files },
} = event;
//ํ์ผ์ ํ๋๋ง ๋ฃ์ ์ ์๊ฒ..^^;;
const theFile = files[0];
console.log(theFile);
};
FileReader API
ํ์ผ๋ฆฌ๋ API๋ ํ์ผ ์ฝ์ด์ ์ ์ฅํ ์ ์๊ฒ ํด์ค๋ค.
const onFileChange = (event) => {
const {
target: { files },
} = event;
//ํ์ผ์ ํ๋๋ง ๋ฃ์ ์ ์๊ฒ..^^;;
const theFile = files[0];
//console.log(theFile);
//1. ํ์ผ๋ฆฌ๋ ์๋ก ๋ง๋ค๊ณ
const reader = new FileReader();
//3. ํ์ผ ์ฝ๊ธฐ ๋๋๋ฉด(reader.onloadend) finishedEvent๋ฅผ ๋ฐ๋๋ค.
reader.onloadend = (finishedEvent) => {
//์ฝ์์ ์ฐ์ด๋ณด๋ฉด finishedEvent.target.result์ ์ด๋ฏธ์ง url์ด ์์ฑ๋ ๊ฒ์ ํ์ธํ ์ ์๋ค.
console.log(finishedEvent);
};
//2. ๋ฆฌ๋์ dataURL๋ก ์ฝ๊ธฐ ๋ฉ์๋๋ก theFile ์ฝ๊ธฐ ์์/ ๊ทธ๋ฌ๋ฉด data ์ป์
reader.readAsDataURL(theFile);
};
console.log(finishedEvent);๋ฅผ ํด๋ณด๋ฉด finishedEvent.target.result์ ์ด๋ฏธ์ง ์ฃผ์๊ฐ ์์ฑ๋ ๊ฒ์ ํ์ธํ ์ ์๋ค.
finishedEvent.currentTarget.result//์ฒจ๋ถํ์ผ readAsDataURL๋ก ๋ฐ์ ๋ฐ์ดํฐ ๋ฃ์ด ๋๋ state
//attachment์ ๋ค์ด์จ url์ ์ฒจ๋ถํ์ผ ๋ฏธ๋ฆฌ๋ณด๊ธฐ img src๋ก ํ์ฉ
const [attachment, setAttachment] = useState();
//์๋ต
//file ๋ฏธ๋ฆฌ๋ณด๊ธฐ ์ ๊ณต
reader.onloadend = (finishedEvent) => {
//์ฝ์์ ์ฐ์ด๋ณด๋ฉด finishedEvent.target.result์ ์ด๋ฏธ์ง url์ด ์์ฑ๋ ๊ฒ์ ํ์ธํ ์ ์๋ค.
//console.log(finishedEvent);
// ์ฒจ๋ถํ ์ฌ์ง ๋ฐ์ดํฐ ๋ค์ด์๋ ์์น: ์ด๋ฒคํธ์ ํ์ฌ ํ๊ฒ์ ๊ฒฐ๊ณผ
const {
currentTarget: { result },
} = finishedEvent;
setAttachment(result);
};
//์๋ต
// attachment๊ฐ ์์ ๋๋ง img ๋์ค๊ฒํ๊ธฐ
{attachment && (<img src={attachment} alt="preview" width="50" height="50" />)}
์ทจ์ ๋ฒํผ์ ๋ง๋ค๊ณ , ์ทจ์ ๋ฒํผ ํด๋ฆญ์(onClick) onClearAttachment ๋ฆฌ์ค๋๊ฐ ์คํ๋๊ฒ ํ๋ค.
setAttachment()์ null์ ๋ณด๋ด img์ ์์ค๋ฅผ ์์ ์ค๋ค.
//์ฒจ๋ถ ์ฌ์ง ์ทจ์ํ๋ ๋ฒํผ
const onClearAttachment = () => {
setAttachment(null);
};
//์๋ต
{attachment && (
<div>
<img src={attachment} alt="preview" width="50" height="50" />
<button onClick={onClearAttachment}>์ทจ์</button>
</div>
)}

setAttachment()์ null์ ๋ณด๋ด ์ฒจ๋ถํ๋ ์ด๋ฏธ์ง์ url์ ์ ๊ฑฐํ์ง๋ง ์์ง file input์ ์ด๋ฏธ์ง ํ์ผ๋ช
์ด ๋จ์ ์๋ค.useRef() ํ
์ ์ฌ์ฉํ์ฌ file input ๋ณ์๋ฅผ ๋ง๋ค๊ณ file input๊ณผ ์ฐ๊ฒฐ์์ผ์ฃผ๋ฉด ๋๋ค.
const fileInput = useRef();
const onClearAttachment = () => {
//1. ์ฒจ๋ถํ์ผ url ๋ฃ๋ state ๋น์์ ํ๋ฆฌ๋ทฐ img src ์์ ๊ธฐ
setAttachment(null);
//2. ์ ํํ๋ ์ฒจ๋ถํ์ผ๋ช
์์ ๊ธฐ
fileInput.current.value = null;
};
//์๋ต
//์ฒจ๋ถํ์ผ ์ ํํ๋ input
//useRef() ์ฌ์ฉ์ํด ref ์์ฑ ์ถ๊ฐ
<input type="file" accept="image/*" onChange={onFileChange} ref={fileInput}/>
//ํ๋ฆฌ๋ทฐ ์ด๋ฏธ์ง
{attachment && (
<div>
<img src={attachment} alt="preview" width="50" height="50" />
<button onClick={onClearAttachment}>์ทจ์</button>
</div>
)}
์ค์ผ์ด! ์ด์ ํ์ผ์ฒจ๋ถ๋ฅผ ์ทจ์ํ๋ฉด ์ฒจ๋ถ๋๋ ํ์ผ๋ช ๋ ์์ด์ง๋ค!