How to display selected input image in ReactJs

 

To display an image after selecting the filename from the local system in a React application, you can make use of the FileReader API. This API allows you to read the contents of a file selected by the user and convert it into a data URL that you can use to display the image.

Here's a step-by-step guide on how to achieve this:

Create a React component that allows users to select a file and display the image:



In your main component (e.g., App.js), use the ImageUploader component:


In this example, we use the FileReader API to read the contents of the selected file and create a data URL for the image. The data URL represents the image as a base64-encoded string, which can be directly used as the src for the <img> tag.

Newest
Previous
Next Post »