Having trouble rendering SVG image using ReactJS

Trying my hand at learning React JS through a tutorial, I created a reusable component to display information. However, I encountered an issue where my SVG image is not displaying properly. Take a look:

https://i.sstatic.net/dhK4D.png

Notice how the image isn't showing and instead, the alt text is displayed.

To accomplish this, I divided the process into three files: 1)index.js - responsible for rendering the ReactJS code

const InfoSection = ({
    lightBg,
    imgStart,
    topLine,
    lightText,
    headLine,
    description,
    buttonLabel,
    img,
    alt,
    id,
    darkText,
  }) => {
return (
        <InfoContainer lightBg={lightBg} id={id}>
            <InfoWrapper>
                <InfoRow imgStart={imgStart}>
                    <Column1>
                    <TextWrapper>
                        <TopLine>{topLine}</TopLine>
                        <Heading lightText={lightText}>{headLine}</Heading>
                        <Subtitle darktext={darkText}>{description}</Subtitle>
                        <BtnWrap>
                            <Button to='home'>{buttonLabel}</Button>
                        </BtnWrap>
                    </TextWrapper>
                    </Column1>
                    <Column2>
                        <ImgWrap>
                            <Img src={img} alt={alt} />
                        </ImgWrap>
                    </Column2>
                </InfoRow>
            </InfoWrapper>
        </InfoContainer>
    )
}
  1. InfoElements - contains all the styled components and CSS. Regarding the issue, ImgWrap and Img are involved.
export const ImgWrap = styled.div`
  max-width: 555px;
  height: 100%;
`;

export const Img = styled.img`
  width: 100%;
  margin-top: 0;
  margin-right: 0;
  margin-left: 10px;
  padding-right: 0;
`;
  1. data.js file- where data is passed
export const homeObjOne = {
    id: 'about',
    lightBg: false,
    lightText: true,
    lightTextDesc: true,
    topLine: 'About',
    headLine: 'headline',
    description:
      'description',
    buttonLabel: 'Get Started',
    imgStart: false,
    img: require('../../images/about.svg'),
    alt: 'alt line, if image does not show up',
    dark: false,
    primary: true,
    darkText: false
  };

I've tried various methods of writing the img in the data file,

img: require('../../images/about.svg')

The path seems correct as there's no error thrown by React. Following the tutorial guidelines, I used require. What am I missing? How can I ensure the image shows up?

Answer №1

I encountered the same issue while working on this exercise.

However, I approached it differently by importing using

import homeObjOneImg from "../../images/svg-3.svg";
and then assigning it to img: homeObjOneImg

import homeObjOneImg from "../../images/svg-3.svg";

export const homeObjOne = {
    id: 'about',
    lightBg: true,
    lightText: false,
    lightTextDesc: false,
    topLine: 'topLine',
    headLine: 'headLine',
    description: 'description',
    buttonLabel: 'buttonLabel',
    imgStart: false,
    img: homeObjOneImg,
    alt: 'alt',
    dark: false,
    primary: false,
    darkText: true
}

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

Iterate over the items stored in localStorage and display a particular value when clicked

For my library project, I am trying to create a shopping cart feature. The issue I'm facing is that when I click on a specific book, I cannot add it to another localStorage. This is my html <!--Knjige--> <div class="container grid" id=&ap ...

Utilize Javascript to input text into a webform

I am working on creating a bot that simulates user input for the WattsApp web website. When I manually type a message into the website, it enables a button that allows me to send the message. However, when my script runs, it finds the input box, changes t ...

Apply a Fade In transition to the ul li elements following a JavaScript toggle

I'm currently experimenting with implementing a Fade in from the right animation for the links displayed in the menu after toggling the menu body in the browser. Despite my efforts, I am unable to determine why the animation is not functioning as expe ...

What is the purpose of having several script tags following the creation of NextJS?

After running next build and next start, my application is still generating many JS files instead of a single entry point. I'm unsure if I missed a step as the documentation suggests this should be all that's required. https://i.stack.imgur.com/7 ...

Accents marked with diacritics are not being detected + An issue occurred while

I'm currently working on putting together a Spanish dictionary by sourcing the definitions from www.rae.es. However, there are a couple of challenges I'm facing: One issue is that the search engine does not function properly with acute accent ...

When YII_DEBUG is disabled, the CSS will not be implemented

My website is built using Yii along with YiiBooster. Everything works fine when I am in DEBUG mode - all styles load and apply correctly. However, when I set YII_DEBUG to false in the index.php file, the CSS files still show as loaded in Firebug, but it ap ...

How should I integrate my JS authentication function in Rshiny to enable the app to utilize the outcome?

Currently, I have an Rshiny application set to be published on the server but in order to ensure that a specific user has access, we require an API authentication token. The process of authentication is handled within JS tags outside of the application, wh ...

Implement checkbox functionality for data binding in Vue framework

I'm having trouble figuring out how to display the selected values in Vue. I've got a form that triggers a query based on the user's selections: <form id="Register"> <br>Firstname<input type="checkbox" value="firstNam ...

Keep the nested ul with absolute positioning open until the pointer is moved outside the element

I am working on a navigation menu where the last list item (li) contains a nested ul for a dropdown menu. My goal is to display this nested menu when hovering over the parent li and hide it when moving away from the menu. However, I am running into an issu ...

React Router problem in library - accessing elements outside the <Router> component

Currently, I am in the process of developing a library that has a dependency on react-router-dom. Within this library, there are various components that utilize elements from react-router-dom such as NavLink. Let's take for instance a component called ...

Tips for determining if a mobile web browser is active on a mobile device's foreground or background

I'm currently developing an angularjs html5 application. Just a quick question - is it possible to utilize pause/resume events in a mobile web application? Alternatively, you can help me by answering my question about how to check if the browser wind ...

Navigating through the concept of passing objects by reference in child components of Angular 2+

Understanding that TypeScript uses object passing by reference can be challenging, especially when dealing with deep copy issues. This becomes particularly cumbersome when working within a theme. I recently encountered an issue with a component containing ...

What is the best way to combine two DIV table rows within Bootstrap 5?

Looking at the code below, there are 6 cells in 2 rows, but I want to merge cell number "2" and "5". I know this can be accomplished using CSS Table DIV, but is there a way to do it with Bootstrap? I haven't found any documentation that worked for me ...

What is the method for obtaining latitude and longitude coordinates within a personally customized tiled Google map?

I have developed a unique Google map with customized tiles, and now I am looking for a way to determine the latitude and longitude coordinates of specific points on the map in order to add markers. My project is based on Google Maps API v3. Currently, I a ...

Bootstrap relies on jQuery for its JavaScript functionality, so jQuery must be loaded before using Bootstrap's JavaScript

I encountered an issue while trying to load the Bootstrap library, consistently receiving this error message: Uncaught Error: Bootstrap's JavaScript requires jQuery Even though I have ensured that jQuery is loaded before attaching the Bootstrap li ...

Delete the display:none; attribute to make the item appear on the screen

The following CSS code styles the element: span { position:absolute; float:left; height:80px; width:150px; top:210px; left:320px; background-color:yellow; display:none; //No display ...

Unable to apply border attributes to AntTable row depending on the record value

I am trying to apply conditional border attributes to certain rows in my AntTable component using React. While I have successfully changed other attributes like background-color, width, and height using rowClassName, I am encountering issues with the bor ...

Struggling to eliminate HTML tags from a string containing the 'aspNetHidden' CSS class

I have a web user control that is being compiled into HTML. The issue I am facing is that the HTML is returning with view states enclosed in DIVs with a class of aspNetHidden. These DIVs are causing problems with my page's design, so I am attempting t ...

Creating a seamless thread using AngularJS

I am working on a Spring MVC application that utilizes HTML and Angular on the client side. I have a method in my Angular controller that needs to run every 5 seconds. How can I achieve this using Angular? Thank you for your assistance. $scope.inspectio ...

Receive information on browser activity

Is there a way to trigger an event when the following actions occur: Pressing the reload icon in the browser Pressing the Back or Forward icon in the browser Selecting the Reload menu item from the browser context menu Selecting the Reload option from t ...