Having trouble getting SPRITE SVG or SIMPLE SVG to show up when using xlinkHref in a React JSX file

I'm struggling to get the SVG logo to display. I created the SVG using icomoon. I'm trying to use xlinkHref="path/to/my/svg" in my jsx code, but it just won't work. Can anyone point out what I might be missing?

Here's the desired effect:

https://i.sstatic.net/aUMsN.jpg

Code:

import React from "react";
import ReactDOM from "react-dom";

import "./styles.css";

function App() {
  return (
    <div className="App">
      <h4>Help me show the logo inside the square:</h4>
      <div className='logo-wrapper'>
        <svg className="logo-one ">
          <use xlinkHref="sprite.svg#icon-ua-logo"/>
        </svg>
      </div>
      <div 
        className='logo-wrapper'
        style={{marginTop: '3rem'}}>
        <svg className="logo-two ">
          <use xlinkHref="./ua-logo.svg"/>
        </svg>
      </div>
    </div>
  );
}

const rootElement = document.getElementById("root");
ReactDOM.render(<App />, rootElement);

Directory Structure

src
  |_ index.js
  |_ styles.css
  |_ sprite.svg
  |_ ua-logo.svg

CSS

.App {
  font-family: sans-serif;
  text-align: center;
}

.logo {
  fill: black;
}

.logo-wrapper {
  height: 10rem;
  width: 10rem;
  background-color: lightgray;
  margin: 0 auto;
}

.logo-one {
  fill: black;
  height: 5rem;
  width: 5rem;
}

.logo-two {
  fill: black;
  height: 5rem;
  width: 5rem;
}

SVG

<svg>
  /*PLEASE REFER TO THE CODESANDBOX LINK BELOW*/
</svg>

For a live demo, check out this codeSandbox link

Answer №1

The solution came from a post I found last year. React and SVG sprite

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

React: Parent signaling child upon completion

and I appreciate any help that can be provided. In my current setup, I have a parent-child component relationship where the state of the parent is passed down as props to the child. Additionally, one of the props includes an onClick event handler which re ...

Tips on adjusting the font size of text in an HTML file displayed through a UIWebView on an iPhone

Currently, I have a bundle of 20 HTML files that all contain text with a font size of 15. While this is suitable for iPhones, the text appears too small on iPads. How can I adjust the font size dynamically within the HTML file using iPhone code to ensure ...

What could be the reason for the malfunctioning of my express delete request?

When I send a delete request using Postman on my localhost, everything functions correctly. However, when trying to make the same request from my React.js client-side, it doesn't go through. Below is the API request: router.delete("/deletetransaction ...

Find the div element that wraps around the currently selected radio button

Take a look at this code snippet: <div class="paymentOption"> <input type="radio" name="paymentOption" value="1" /> <span class="marginLeft10">1-time payment using a different credit card</span> </div> Is it feasible ...

Eliminate styling that is specific to certain browsers

Recent browser updates have introduced new styling and functionality to input[type=number], including up and down buttons. In Chrome, I was able to eliminate this added styling by accessing the shadow DOM and identifying the specific element. However, de ...

Add some text onto the Box Component in Material UI

I am looking to replicate the hover functionality (UI) seen in this example: Desired UI Source: Since adjusting background image styles can be complex, I have opted to use the Box Component from Material UI. Within the Box Component, I have implemented th ...

Why does one of the two similar javascript functions work while the other one fails to execute?

As a new Javascript learner, I am struggling to make some basic code work. I managed to successfully test a snippet that changes text color from blue to red to ensure that Javascript is functioning on the page. However, my second code attempt aims to togg ...

How to access the CSS and JS files in the vendor folder using linemanjs

Currently, I am in the process of developing a web application utilizing linemanjs. However, I have encountered an issue where the vendor css and js files are not being referenced correctly when explicitly included. I would appreciate any guidance on what ...

No contains operator found in Material UI Datagrid

While working on a project, I utilized Material UI's datagrid and successfully implemented filters such as contains and isEmpty. However, I am struggling to find information on how to create a notContains filter. Does Material UI natively support this ...

The style in App.js was rejected and the script was not executed as intended

element, I have encountered a common issue that many others seem to face. The typical solution involves correcting a line of code like this one: app.use(express.static(__dirname + '/public')); Unfortunately, despite my efforts to follow the ad ...

Recommendations for a UI library specializing in displaying analytical graphs

Looking to create analytical graphs of tweets similar to the site found at the following link Website-Link Curious about the top open source options available for this task? I've heard good things about Raphael.js. Any other recommendations? ...

What is causing the issue with the "Inline-block" property in this CSS code?

Please review the CSS code provided below for styling instructions. /*rex is the container of ex,ex2,ex3*/ div.rex{ height:200px; border:0px; margin:60px auto; padding: 0; vertical-align:top; } div.ex{ width:34%; height:200px; background-color:#4f443a; ...

When trying to reference "this" and store it in a variable, it appears as undefined. However, DevTools show that it is actually defined

I've encountered an unusual situation in my React app involving the binding of "this" - I have a function within a component named "App" that is located in a separate file. In the main file, I've bound the "this" command to it. What's puzzl ...

MUI Alert: Encountered an Uncaught TypeError - Unable to access properties of undefined when trying to read 'light' value

After utilizing MUI to create a login form, I am now implementing a feature to notify the user in case of unsuccessful login using a MUI Alert component. import Alert from '@mui/material/Alert'; The code snippet is as follows: <CssVarsProvide ...

What could be causing the error message "setShowModal is undefined" to appear in my React project?

Here is the code snippet I am working on: import React, { useState } from "react"; import Modal from "./Modal"; function displayModal() { setShowModal(true); } export default function NewPostComponent() { const [showModal, setShowMod ...

What can be done to resolve the issue of text not displaying correctly on the screen?

I am attempting to position text to the right of a card header in my project, but for some reason the working example on fiddle is not working. I am using Bootstrap 4. Check out the example here. Here is what I am seeing in my app: https://i.sstatic.net ...

What is the best way to choose all the checkboxes in a checkbox list, such as a ToDo List?

I am currently working on developing a to-do list where each item includes a checkbox. My goal is to be able to select these checkboxes individually by clicking on them. Furthermore, I would like the checkboxes to toggle between being checked and unchecked ...

State not properly updating in the most recent version of Next.js

"use client"; import { useState , useEffect } from "react"; import React from 'react' function form() { const [name, setName] = useState(""); const [email, setEmail] = useState(""); const [disable, ...

Determine if a user is signing in for the first time with Next-auth

Is there a way to verify if it's the user's initial sign-in with Next-Auth? I attempted to set up the newUser page as per the official documentation, but it doesn't appear to be functioning correctly. This is how my pages are configured in ...

The level of transparency linked with text in a blockquote

I've spent hours on this and I'm completely lost. Even though I believe my code is correct, it keeps telling me it's wrong. I don't know where to go from here. The task requires adding a style rule for the blockquote element that chan ...