Having trouble getting my images to load in React. Seems like a common issue that many people run into

UPDATE: After trying different paths for my image import (such as 'import Github from '../..img/github.png'), the errors have disappeared but the image still won't load on my app.

Folder Structure: My-Portfolio node_modules public src components Intro Intro.jsx Intro.css NavBar NavBar.jsx NavBar.css img App.css App.js index.css index.js

Hey everyone, please bear with me. I'm new to this and I know this question has been asked multiple times. I've spent an hour searching and experimenting before turning to this forum for help.

So I have a file called Intro.jsx and I'm attempting to include the GitHub logo which is located in a folder named 'img' inside my 'src' folder but outside my 'components' folder.

I've attempted moving the img folder to different locations but I keep encountering the same error.

Here are the different import paths I've tried:

import Github from '../../img/github.png'

import Github from '../img/github.png'

import Github from './img/github.png'

import Github from '../src/img/github.png'

I've also tried other variations. But I just can't seem to get it to load. The error message remains consistent each time.

const Intro = () => {
    return (
        <div className="intro">
            <div className="i-left">
                <div className="i-name">
                    <span>I am </span>
                    <span>Josh!</span>
                    <span>
                        Lorem ipsum dolor sit amet consectetur adipisicing elit. Ex,
                        doloribus? Explicabo, itaque? Sit adipisci odio mollitia veritatis
                        hic laudantium quisquam itaque ad
                    </span>
                </div>
                <button className="i-button button">Hire Me</button>
                <div className="i-icons">
                    <img src="{Github}" alt="" />
                    <img src="{LinkedIn}" alt="" />
                    <img src="{Instagram}" alt="" />
                </div>
            </div>
            <div className="i-right">I am right side</div>
        </div>
    );
};

Current Error:

ERROR in ./src/components/Intro/Intro.jsx 6:0-39

Module not found: Error: Can't resolve '../img/github.png' in '/Users/josh/Desktop/my-portfolio/src/components/Intro'

How can I resolve this issue? What am I missing? Thank you for your understanding and assistance!

I wanted to upload an image but couldn't due to being new here.

Answer №1

My braces were misplaced. The proper JSX syntax is

<Component prop={<expression>} />
, so in my scenario, this is the correct code:

<img src={Github} />`

Alternatively, you can utilize template strings:

<img src={`${Github}`} />

This was the issue I encountered after the React update.

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

Looking to make an API call with every keystroke in a React application, however, the response may contain a large number of objects

Currently, I am utilizing react and axios for the frontend while relying on nextjs with prisma for the backend. Within my database, there are 4000 exercises related to fitness stored as data. My objective is to create a function where each keystroke trigge ...

inserting picture within the code

Looking for some feedback on this: I decided to use a third-party image host to create the code (I've used them in the past). However, when I added the code to my page where I intended it to be (after verifying in Google Inspect), the image does not a ...

Error with JavaScript slideshow The slideshow using JavaScript seems to

I'm currently utilizing a script from the WOW Slider (free version) that looks like this: var slideIndex = 0; function showSlides() { var i; slides = document.getElementsByClassName("mySlides"); dots = document.getEle ...

How to modify a nested object in MongoDB based on the JSON data provided

In my possession, there exists a structured form of JSON data as displayed below: [ {"size":100,"year":2015,"geography":"London","age":"21","gender":"Female"}, {"size":80,"year":2015,"geography":"Cardiff","age":"38","gender":"Male"}, {"size":80,"year":201 ...

Incorporating photos within the layout of a Twitter Bootstrap grid

Looking to showcase images like the ones found here: https://i.stack.imgur.com/eD4GD.jpg These images are original and come in various sizes. I want a hover effect that includes blur, fogging effects, and text placed in the middle of the picture. Check ou ...

Basic click event triggered every second click in JavaScript and HTML

onclick="HandleAction(\'playnow\');HandleAction(\'stop\');" This element performs two functions simultaneously. However, it only executes the actions \playnow\ and then \stop\ immediately after. ...

What could be the reason for the lack of a border below the navigation bar in this code?

I am attempting to create a navigation bar with a simple thin border that becomes thicker when hovered over. Although I have written the code below, it seems to not be working as intended. How can I fix this issue? #top-menu ul li a { display: block ...

The submit function in Jquery is not functioning properly within the success callback of an Ajax request

After successfully submitting a form in AJAX using POST, I receive a new form that needs to be automatically submitted in jQuery. However, for some reason, the .submit() function seems to be ignored and I can't figure out why. I've tried adding ...

The style from 'http://localhost:2000/cssFile/style.css' was rejected because its MIME type was 'text/html'

Currently, I am attempting to include my style.css file in the home.ejs file being rendered by express.js. However, I keep encountering the following error: Refused to apply style from 'http://localhost:2000/cssFile/style.css' because its MIME t ...

Select a Month and Year with Material UI React

Can we create a month and year picker using Material UI that displays only the month and year (e.g. 2020-09) in the output? ...

Interactive jQuery tabbed interface with drop-down selectors, inconsistent display of drop-down options when switching between tabs

After receiving assistance from members oka and Mike Robinson, I have successfully created two tables for users to interact with using tabs and dropdowns. Both tables have the same structure and feature a dropdown menu to show/hide columns. Users can sele ...

Turn on the text field when the enter key is pressed

I've searched online for solutions, but none of them have resolved my issue. Upon loading my page, I am able to successfully have my JS file select the first textfield. However, I am struggling with getting it to proceed to the next textfield when th ...

Matching objects in an array based on a specific property using the key of another object

To display information in the UI based on matching values between the data.examples array object's name.value property and the keys in the wcObject.notCoveredList, we need to create a logic. If there is a match, all corresponding values from wcObject ...

Managing backslashes during the processing of JSON

I am currently facing an issue with a JSON string that I am trying to parse using the JSON.parse method. The problem seems to be related to the presence of backslashes and parentheses in the string, which results in an 'invalid character' error. ...

Guide to fixing Vue app crashes caused by Web Sockets error

I'm encountering an issue with my Vue application as I am unable to load a specific page due to the app crashing. The console displays the following error, even though there is no implementation of Web Sockets in the app. Despite adding this snippet ...

When making an API request, the response includes the body of the data, however, the specific properties cannot be

I've encountered an unusual bug while using the Mapbox geocoding API. const geocode = async (address, callback) => { const url = `https://api.mapbox.com/geocoding/v5/mapbox.places/${address}.json?access_token=token&limit=1` try { ...

Expanding Drop-Down Feature in CodeIgniter: How to Create Nested Dropdown Menus

I'm working on a dropdown menu that is populated with items using a foreach statement. When an item is selected, I need another dropdown menu to appear where specific items can be specified. First Dropdown - Categories (Completed) When a Category is ...

Utilizing React Navigation to pass custom information to the nested screen

I'm currently trying to understand how to use react-navigation for navigating between screens in react-native. To begin, I set up a stacked view like this: import { createStackNavigator } from 'react-navigation'; import Home from './h ...

Guide to using get() and res.sendFile() function to redirect webpages

Why is the page not redirecting properly? In my index.html file, I have this script: $.get( "/loginPage", function( data ) {}); The purpose of this script is to check if a user is logged in. If they are, it should redirect them to the lobbyPage. This is ...

The functionality of the JavaScript animated placeholder seems to be malfunctioning

I am currently working on a code that updates the placeholder text every 2 seconds. The idea is to have it type out the letters one by one, and then erase them in the same manner. Unfortunately, the code is not functioning as expected. As a newcomer to J ...