DEV Community

Cover image for HOW TO PREVIEW AN UPLOADED IMAGE WITH REACT.JS
Candie
Candie

Posted on • Edited on

HOW TO PREVIEW AN UPLOADED IMAGE WITH REACT.JS

As a developer, if your file upload component is a customized one, or you want users of your platform to be able to preview their uploaded image, i am going to explain how you can achieve that in few steps.

REQUIREMENT:
create a react project using this command:
npx create-react-app image-upload-preview

  • STEP 1 Create a react component
import react from 'react'

export default function ImagePreview(){
return <div></div>
}
Enter fullscreen mode Exit fullscreen mode
  • STEP 2 Add a file input component
*import react from 'react'*

export default function ImagePreview(){
return <div>
   <input type='file' name='image' />
</div>
}
Enter fullscreen mode Exit fullscreen mode
  • STEP 3 install this library to preview your uploaded image

npm install mrcandie-react-preview

import react from 'react'
import Preview from 'mrcandie-react-preview'

export default function ImagePreview(){
return <div>
      <input type='file' name='image' />
      <Preview />
</div>
}
Enter fullscreen mode Exit fullscreen mode

STEP 4

Extract uploaded image file using react useState hook

import react, {useState} from 'react'

export default function ImagePreview(){
const [image, setImage] = useState('')
return <div>
      <input type='file' name='image' onChange={(e)=> 
            setImage(e.target.files[0]} />
            <Preview />
</div>
}
Enter fullscreen mode Exit fullscreen mode

STEP 5
Pass the uploaded image/file as a prop to the Preview component

import react, {useState} from 'react'

export default function ImagePreview(){
const [image, setImage] = useState('')
const [imageUrl, setImageUrl] = useState('')
return <div>
      <input type='file' name='image' onChange={(e)=> {
            setImage(e.target.files[0]} />
            }
       <Preview file={image} />
</div>
}
Enter fullscreen mode Exit fullscreen mode

FINAL STEP
Voila! Your uploaded image is previewed;

import react, {useState} from 'react'

export default function ImagePreview(){
const [image, setImage] = useState('')

return <div>
      <input type='file' name='image' onChange={(e)=> {
            setImage(e.target.files[0]} />
            }
      <Preview file={image} />
</div>
}
Enter fullscreen mode Exit fullscreen mode

read more about mrcandie-react-preview documentation here

Sentry blog image

How to reduce TTFB

In the past few years in the web dev world, we’ve seen a significant push towards rendering our websites on the server. Doing so is better for SEO and performs better on low-powered devices, but one thing we had to sacrifice is TTFB.

In this article, we’ll see how we can identify what makes our TTFB high so we can fix it.

Read more

Top comments (0)

Billboard image

Create up to 10 Postgres Databases on Neon's free plan.

If you're starting a new project, Neon has got your databases covered. No credit cards. No trials. No getting in your way.

Try Neon for Free →

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay