Invalid file location of original image

I'm experiencing an issue where I am unable to capture the input image from my device.

Instead, it keeps showing me a default image that I have set and provides a fake path for the input.

Can someone help me solve this problem and provide some suggestions?

Thank you!

JavaScript:

const [Image, setImage] = useState('')

    const fetchImage = async (e) => {

        // console.log('e is ',e)
        try {
            const response = await import('./img/noprofil.jpg')

            console.log('res is ', response)
            setImage(response.default)
            console.log("image in response is " + response)
        }
        catch (err) {

            console.log('error is ', err)
        } finally {
        }
    }

HTML:

 <div className="col-3 ">
  <div className="container">
    <div className="img-holder">
      <img src={Image} className="rounded" alt="" />
    </div>
    <input type="file" accept="image" name="image-upload" id="input" onChange={fetchImage} />
    <div className="label">
      <label className="image-upload" htmlFor="input"> Change </label>
    </div>
  </div>
</div>

Answer №1

In case you're unsure about how to incorporate local images in React, I've put together a generic example. Simply place the image in the public folder and access it directly within your app. To provide further clarity, I've created a straightforward codesandbox demonstration.

https://codesandbox.io/s/react-image-public-l03rc7

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Error encountered in Next JS: EPERM - Unable to delete directory due to lack of permission

My Next.js application was running smoothly until I encountered this error upon returning to it: errno: -4048, code: 'EPERM', syscall: 'rmdir', path: 'C:\\Users\\gmacr\\Google Drive\\ ...

Scraping Information on Pokemon from the Web

I am currently researching the number of moves each Pokemon from the first generation could learn. After some exploration, I came across a helpful website that provides this information: The website lists 151 Pokemon, and for each of them, their move set ...

What is the reason for nesting divs within each other?

Can anyone help me troubleshoot this Django template code? I have noticed that the main-card-faq div is being rendered inside the main-card div even though it's clearly outside. Any thoughts on what might be causing this issue? {% extends 'base.h ...

Visibility of image changes after selecting from dropdown menu

Whenever I choose a language from the dropdown menu, the corresponding language image should display in a div next to it, while hiding the irrelevant images. I've been experimenting with CSS properties like display: none; and block;, as well as jQuery ...

What is the best way to display a border around text in an HTML element when the mouse hovers over it?

I am currently developing a Browser Helper Object (BHO) for Internet Explorer that involves searching for specific words on a web page and encasing those words in an HTML tag for styling purposes. While I have code in place that allows me to change the st ...

Interact with a modal element using puppeteer

I'm having trouble clicking on the email login button inside a modal using Puppeteer for automation. The code is not able to find the modal element. Can someone assist me in debugging this issue? const puppeteer = require('puppeteer'); ( ...

interaction & portal

I am currently managing a website that includes an iframe. Both the main site and the embedded site within the iframe are verifying the $_SESSION["login"]. Therefore, if the session expires, the user will be automatically logged out. The issue I'm fa ...

Show the output of a MySQL query on a modal, alert, or popup box within a webpage initiated by a search box, with the option to close the

Having worked in IT for 16 years, I am new to coding and seeking help on displaying search results from an input HTML box. I want the data from a MySQL database to be displayed in a modal, alert, or popup box with a closing "X" button. This database only c ...

When the parent passes the useState to the child and the child uses it, the child remains static and does not re-render

It seems like the parent component does not trigger a re-render of the child component when the useState is updated. Normally, using useState should automatically cause a re-render, right? Unless I am missing something... index.js import { useEffect, useS ...

The "Overall Quantity" of items will vary as it goes through different numerical values, despite the fact that I employed --

I am currently working on an e-commerce website with a shopping cart feature. The cart displays the number of items added to it, which increases by one when 'Add to Cart' is clicked and decreases by one when 'Remove' is clicked. However ...

Removing an element from an array using JavaScript with two arrays

Let's consider a scenario where we have two arrays: const names = ["Alice", "Bob", "Charlie"] const exclude = ["Bob"] The expected result would be: names = ["Alice", "Charlie"] As a beginner in javascript, I would prefer a solution using plain jav ...

Currently studying PHP. The classic "Hello world" message is not appearing when using EasyPHP and coding in Notepad++

<html> <!--HTML--> <head><title>a simple test</title></head> <body>a basic test</body> <p>javascript</p> <!--javascript--> <p><script> document.write("hello universe") </sc ...

Enhance JSON object with texture using three.js

I've successfully created a 3D model using Blender. After exporting it with the Three.js exporter and loading it in JavaScript, everything seems to be working fine except for one issue - the texture is not being displayed properly. I made sure to add ...

What is the total number of webgl.draw calls generated by Three.js when rendering a specific quantity of geometries, materials, and meshes?

Can you help me determine the total drawArrays/drawElements calls required by THREE.renderer.render(scene, camera)? I speculate one call per geometry, incorporating attributes for materials/mesh properties. Is my assumption accurate or am I overlooking ce ...

It is important for the CSS :active state to transition to an "inactive" state after a certain period of

For mobile, I utilized the :hover and :active pseudo-classes to create a transition effect when tapping on a tab that enlarges the elements for more information. However, I am seeking a way for the activated element to return to its original state after a ...

One way to increment a database attribute by one is to send a GET request to the database using Reactjs

I am currently working on a Mernstack project and I have a model called Events. Each Event has attributes such as title, description, startingDate, and closingDate. One of the functionalities I want to implement is allowing users to indicate that they are ...

Increasing a value within HTML using TypeScript in Angular

I'm working with Angular and I have a TypeScript variable initialized to 0. However, when trying to increment it using *ngFor in my .ts file, the increment is not happening (even though the loop is running correctly). my-page.html <div *ngFor=&quo ...

Having trouble making my Navbar fully responsive for mobile. Can't seem to get the Nav-links to display correctly. Any advice on what

I need some help adjusting my navbar to fit on mobile screens. While I can make the logo shrink, I'm struggling to change the size of the links so that the navbar doesn't get cut off. Below is the code for my Navbar and the corresponding CSS. N ...

Refreshing the page in Next.js causes issues with the CSS classNames

I am currently in the process of migrating a React SPA to Next.js, and I am relatively new to this framework. The issue I am encountering is that when I initially load the Home page, everything appears as expected. However, if I refresh the page, incorrect ...

Tips for executing an npm command within a C# class library

I am currently developing a project in a class library. The main objective of this project is to execute a JavaScript project using an npm command through a method call in C#. The npm command to run the JavaScript project is: npm start The JavaScript ...