Skip to content

Instantly share code, notes, and snippets.

@ejirocodes
Last active August 30, 2021 09:01
Show Gist options
  • Save ejirocodes/a967b14f80a7ba585ba1bd43fb6e6949 to your computer and use it in GitHub Desktop.
Save ejirocodes/a967b14f80a7ba585ba1bd43fb6e6949 to your computer and use it in GitHub Desktop.
import React, { useState } from 'react';
export default function Image() {
const [imgUrl, setImgUrl] = useState('');
const [imgUpload, setImgUpload] = useState('');
const handleImageChange = (e) => {
console.log(e.target.files[0]);
// use this to display the image on the DOM
setImgUrl(URL.createObjectURL(e.target.files[0]));
// this is what you will send to the backend
setImgUpload(e.target.files[0]);
setImgUrl(e.target.files[0]);
};
const handleImageUpload = async () => {
try {
let formData = new FormData();
formData.append('photo', imgUpload);
let { res } = await axios.post('url', formData);
console.log(res);
} catch (error) {
console.log(error);
}
};
return (
<div>
{imgUrl ? (
<img src={imgUrl} alt="profile" />
) : (
<img
src="https://cube.elemecdn.com/3/7c/3ea6beec64369c2642b92c6726f1epng.png"
alt="profile"
/>
)}
<form action="">
<label htmlFor="image">Upload</label>
<input
type="file"
name="image"
id="image"
accept="image/*"
style={{ display: 'none' }}
onChange={handleImageChange}
/>
<button onClick={handleImageUpload}> Upload</button>
</form>
</div>
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment