What could be causing the image not to load in this code with the img tag?

import React from 'react';

import styled from 'styled-components';

function Navbar() {
    return (

        <Container>
            <img src='./appleWhite.png' />
        </Container>
    )
}

export default Navbar

const Container = styled.div`
    height: 24px;

Answer №1

To incorporate an image into your project, you must first import it.

import myImage from './appleWhite.png'

<img src={myImage} />

If importing the image is not preferred, you can place the image in the public folder and use it like this:

<img src={'/appleWhite.png'} />

During runtime, your React code gets bundled and imported as scripts. What you see is the index.html file. There is no src folder during runtime, which is why your original code didn't work as expected.

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

Flexbox or Bootstrap 4 Grid: Which is the Better Choice?

My form is structured as follows: <div class="row"> <div class="col"> <textarea name="comment" class='form-control' placeholder='Type new comment here..'></textarea> </div> <div clas ...

Why is my CSS and Bootstrap full-page image not displaying properly?

I've been struggling to get a full-page image to display on my website and resize responsively across different screens. I've searched through w3schools and Stack Overflow for solutions, but no matter what I try, it just doesn't seem to work ...

Display the div if the input field is left blank

Is there a way to display the div element with id="showDiv" only if the input field with id="textfield" is empty? <form action=""> <fieldset> <input type="text" id="textfield" value=""> </fieldset> </form> <div id="sh ...

Adding variables to a div using jquery

Is there a way to use jQuery to append variables to a div? Below are my codes, but I am looking to display div tags in the append. For example: .append("" + p + "") var image = item.image; var label = item.label; var price = item.price; ...

After the AJAX call is finished, the height of the div does not adjust accordingly

My goal was to increase the height of the "scroller" div and place it at the end of the success call back function. Within this callback, I dynamically insert some inner divs into the "scroller" div. The default height of the scroller div is 400px. If it ...

What is the best way to toggle button visibility upon clicking in AngularJS?

I have 4 buttons in total. Initially, only one button will be visible while the remaining three should be hidden. I attempted to create this scenario using an example fiddle, but the code was not ideal. I am looking to utilize a ternary operator and simp ...

Prevent the dropdown from closing after clicking on a href link

Is there a way to ensure that my dropdown remains open even after a page reload? I'm looking for a solution where clicking on an item(href) within the dropdown will keep it open after redirection. I've tried using the jQuery method called stopPr ...

Ways to showcase a textbox input in different DIVs multiple times

I am struggling to showcase the content of a textarea within multiple div elements when a user inputs text. The value only appears in the first div, but I want it to display in every div. Here is my HTML and jQuery code: <input type="textarea" id="my ...

Creating Dynamic Website Titles with PHP and HTML

I am currently teaching myself web development and using the W3 validator to check my code. I have a document that loads like this: <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <meta http-equiv="Conten ...

Animation using CSS that starts with a horizontal slide and transitions into a diagonal slide

Can I create an animation to move a graphic image on an html page from the middle of the screen to the top left corner? Here is what I have so far: #img1 {
 bottom: 50%;
 display: block;
 position: absolute;
 a ...

Django/React CSRF validation error: Origin verification failed - the URL http://localhost:8000/ does not correspond to any authorized origins

My current project involves the development of a web application using Django, RestApi, and ReactJs. However, I've encountered an error when sending a POST request from a form which reads: "CSRF Failed: Origin checking failed - http://localhost:8000/ ...

JavaScript not redirecting to HTML page as expected

I have developed a basic login page that makes an Ajax request to the backend. However, I am experiencing difficulties with redirecting the page upon successful login. The redirect function only seems to work 1 in 15 times, and I'm unsure of the reaso ...

ran into the error that this state is not a function

Every time I run my code, I encounter the error message this.setState is not a function. Can anyone offer guidance on how to fix this issue? Thank you! spiele.listUsers(params, function(err, data) { if (err) console.log(err, err.stack); else con ...

React Native: FlatList child component does not automatically re-render when setState() is used

I recently came across a tutorial on implementing swipe right on a React Native FlatList (found here). While the tutorial works as expected, I am struggling to grasp why the FlatList component needs an associated prop with state in order to trigger a re-r ...

There is a table equipped with two buttons. When using jQuery-ajax, rather than replacing the <tbody> of the <table>, it redirects the browser to the specified ajax URL

I initially simplified my code to troubleshoot, but I now realize I need to show the unsimplified code to pinpoint the issue. I am replacing the HTML and JavaScript with the "full version" and have updated my description with more details. I have a table ...

Retrieving information from an Express.js API using React.js. Postman requests are successfully communicating with the API

Hey there, I'm facing a little issue and could use some help. I have an Express application that is working perfectly with requests from Postman (adding, deleting, etc.). Now, I want to connect my client side (React.js) with the existing API using the ...

I am experiencing an issue with my css dropdown menu appearing incorrectly on both Internet Explorer and Firefox

Chrome and Safari are working perfectly fine, but I encountered an issue with IE and FF where the submenu menu appears on the left side of the main navigation. The website in question is cedumilam.php.cs.dixie.edu. Below is the CSS code I am using: # ...

Having trouble displaying image using absolute path in Express

Currently, I am developing a NodeJS application and have encountered an issue with a div element that has a background-image set in an external CSS file. Surprisingly, the CSS file functions perfectly when tested independently, and the background image dis ...

Exploring the process of iterating through a nested array in Firebase data structures

Here is a visual representation of the array JSON data image for reference I'm trying to extract the image data and display it within my React component. I attempted using a map inside a map function, but encountered issues. Refer to the image above ...

Modify the color of the text input by the user in an AJAX-enhanced text box

After successfully implementing an autocomplete textbox using AJAX Autocomplete, I decided to enhance the feature with some Fuzzy logic. Now, as the user enters 3 characters, my database returns a list of records that match those characters. The search re ...