20
loading...
This website collects cookies to deliver better user experience
npx create-react-app simple-file-uploader
in the terminal.npm install react-simple-file-upload
, and voila- we have it ready to use.cd YOUR_PROJECT_NAME
and then run npm run start
. Boom! Let’s go!npm run start
// App.js
import './App.css';
function App() {
return (
<div className="App">
<header className="App-header">
</header>
</div>
);
}
export default App;
// App.js
<header className="App-header">
<h1>Simple File Upload Demo</h1>
<a className="btn" href="https://simplefileupload.com">
Try it now!
</a>
</header>
import SimpleFileUpload from ‘react-simple-file-upload’
back in our App.js file.main
tag to use as a wrapper for the uploader and our grid.// App.js
import './App.css';
import SimpleFileUpload from 'react-simple-file-upload';
function App() {
return (
<div className="App">
<header className="App-header">
<h1>Simple File Upload Demo</h1>
<a className="btn" href="https://simplefileupload.com">
Try it now!
</a>
</header>
<main>
<div className="upload-wrapper">
<SimpleFileUpload
apiKey="YOUR_API_KEY_GOES_HERE"
onSuccess={handleUpload}
preview="false"
/>
</div>
</main>
</div>
);
}
export default App;
handleUpload
doesn’t exist. Let’s fix that.function handleUpload(url) {
console.log(url)
}
// App.js
import './App.css';
import SimpleFileUpload from 'react-simple-file-upload';
function App() {
function handleUpload(url) {
console.log(url)
}
return (
<div className="App">
<header className="App-header">
<h1>Simple File Upload Demo</h1>
<a className="btn" href="https://simplefileupload.com">
Try it now!
</a>
</header>
<main>
<div className=’upload-wrapper’>
<SimpleFileUpload
apiKey=’YOUR_API_KEY_GOES_HERE’
onSuccess={handleUpload}
preview="false"
/>
</div>
</main
</div>
);
}
export default App;
import { useState } from ‘react’
at the top of our file. After that we’ll create the hook just below the line function App() {
.const [uploadedImages, setUploadedImages] = useState([])
.function handleUpload(url) {
setUploadedImages([...uploadedImages, url])
}
.upload-wrapper
div<ul className="image-grid">
{uploadedImages.length ? (
uploadedImages.map((image) => (
<li>
<img src={image} alt="Fun images" />
</li>
))
) : (
<p>Your uploaded images will appear here!</p>
)}
</ul>
.App header {
padding: 1rem 4rem;
display: flex;
justify-content: space-between;
align-items: center;
}
.App header h1 {
font-size: 1.25rem;
}
.btn {
background: linear-gradient(90deg, #b731dd 0, #4161eb 100%);
padding: 0.5rem 1.25rem;
border-radius: 0.25rem;
text-transform: uppercase;
font-weight: bold;
transition: 150ms cubic-bezier(0.6, -0.28, 0.735, 0.045);
box-shadow: 5px 5px 10px rgba(17, 17, 17, 0.25);
}
.btn:hover {
transform: scale(1.05);
}
.App header a {
color: white;
text-decoration: none;
}
.App main {
max-width: 1024px;
margin: 0 auto;
overflow: hidden;
}
.App .upload-wrapper {
display: flex;
justify-content: center;
}
.App main .image-grid {
display: grid;
grid-template-columns: repeat(3, minmax(0, 1fr));
gap: 2rem;
row-gap: 6rem;
list-style: none;
margin-top: 6rem;
text-align: center;
padding-left: 0;
}
.image-grid p {
grid-column-start: 2;
opacity: 50%;
}
.image-grid li {
overflow: hidden;
height: 200px;
}
.image-grid img {
width: 100%;
height: 100%;
object-fit: contain;
object-position: center;
}