show uploaded image in react/nextjs

To show an uploaded image in React/Next.js, you can follow these steps:

  1. Import the necessary modules: First, you need to import the required modules in your component. You will need the useState and useEffect hooks from React and the Image component from Next.js.
import React, { useState, useEffect } from 'react';
import Image from 'next/image';
  1. Set up the state for the image URL: Inside your component, declare a state variable to store the URL of the uploaded image.
const [imageURL, setImageURL] = useState('');
  1. Handle the image upload: Create a function that handles the image upload. This function will be triggered when the user selects an image file.
const handleImageUpload = (event) => {
  const file = event.target.files[0];
  const imageURL = URL.createObjectURL(file);
  setImageURL(imageURL);
};
  1. Render the image upload input: Add an input element to your component's render method. Set the type attribute to "file" and add an onChange event listener to call the handleImageUpload function when the user selects an image.
<input type="file" onChange={handleImageUpload} />
  1. Display the uploaded image: Finally, use the Image component from Next.js to display the uploaded image. Pass the imageURL state variable as the src prop of the Image component.
{imageURL && <Image src={imageURL} alt="Uploaded Image" />}

By following these steps, you will be able to show the uploaded image in your React/Next.js application.