Updating the background SVG display in JavaScript/React according to the current state value

I am currently immersed in a project for my Bootcamp that involves creating a functional product with an aesthetically pleasing front end. I have chosen to utilize a .SVG as the background, set via CSS, and have successfully managed to alter the displayed SVG based on CSS media queries. However, I am intrigued by the idea of changing the background based on a value within the state/props.

The core concept of this venture revolves around an image viewer combined with a user rating system. My vision entails altering backgrounds according to the category of the photo, which is stored as a value within the array containing the photo/user data. For instance, if the category is "Dog," I wish to showcase a tailored background. Conversely, when transitioning to another photo belonging to the "Cat" category, I aim to display a distinct background. The animated SVGs used are crucial components of these backgrounds.

In order to execute this, I believe incorporating the code into the existing functions responsible for switching between displayed photos would be the most effective approach.

Your guidance on this matter would be greatly appreciated!

Answer №1

You have the option to directly apply styles based on the state value.

Another possibility is to utilize the ReactSvg package, which grants you access to nodes within an SVG.

<ReactSVG beforeInjection={(svg) => { 
    const myStyleClass = this.state.myStyle == 'x' ? 'y' : 'z';
    svg.classList.add(''); 
}} src={myImportedImage} />

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

What is the method for incorporating a dotted line preceding the menu options?

I am currently working on an asp.net menu that looks like this: <div id="left"> <div id="menus" runat="server"> <div id="left_menu"> <div class="abc"> < ...

Sticky Position Not Functioning Properly in Flexbox Layout

I've attempted to use align-self: flex-start, but it had no effect. The main flex container is on the right side of my page (flex-direction: column) and I want one block of flex columns to stick once I scroll down to them. I enclosed a container aroun ...

Difficulty using Tailwind CSS classes in React following setup

I have encountered an issue where the Tailwind CSS classes are not displaying properly, despite following the installation process outlined on tailwindcss.com for create-react-app. I have double-checked everything and cannot seem to identify why it is stil ...

Challenge with the contrast of text color on dark mode with a backdrop of light color texture (image)

Currently, I am using Bootstrap 4 and I have set a light coloured .png image as the background for the navbar and body. The footer area has a dark background with off-white text colour. By default, the text colour is black to complement the light-coloured ...

Please explain the display property used in Bootstrap input groups

I have been working on implementing a button that toggles the visibility of a datepicker when clicked. <a class="btn btn-primary btn-lg mr-5" href="../Stores/stalls.html">Check Current Operating Stalls </a> <div style="vertical-align: t ...

Is it possible to customize the color of an SVG image within Next.js using next/image?

import Image from 'next/image' ... <Image src="/emotion.svg" alt="emtion" width={50} height={50} /> I am trying to change the color of the SVG using next/image. However, applying the following CSS rule does not seem ...

Monitoring the scroll position of a React component

I am working on a component that contains a main tag with the CSS property of overflow: auto. Depending on the scroll height of this element, I need to render a different element. How can I determine the scroll position of an element in React? To give you ...

Is there a neat method in React and Material UI for de-structuring the props that I am passing to useStyles?

When passing props to useStyles based on the Material docs, our code looks like this: const useStyles = makeStyles({ // style rule foo: props => ({ backgroundColor: props.backgroundColor, }), bar: { // CSS property color: props => ...

What is the best way to remove table cells from a TableBody?

Currently, I am in the process of designing a table to display data retrieved from my backend server. To accomplish this, I have opted to utilize the Table component provided by Material UI. The data I retrieve may either be empty or contain an object. My ...

How to style a div's height responsively behind a circle using CSS

Can a CSS pattern be created with responsive design, ensuring that the background line is always in the correct position and height regardless of window size? https://i.sstatic.net/27Tf2.jpg ...

Preserve the Redux state when transitioning from a popup to the main window

My authentication process involves using auth0 to authenticate users. After selecting a provider, a popup opens for the user to choose an account to sign in with. With the help of the Auth0 sdk, I am able to retrieve user information such as email and nam ...

React's latest project fails to recompile upon saving

After setting up a new react app with create-react-app, I ran npm start to compile the app and view the hello react page on localhost://3000. However, making changes to the project and saving them doesn't seem to have any effect. Upon checking the Ch ...

Is it possible to generate multiple modal windows with similar designs but varying content?

I am facing a challenge with 140 link items that are supposed to trigger a modal window displaying a user profile for each link. The issue is that each user profile contains unique elements such as three images, a profile picture, text paragraph, and socia ...

Exploring React hooks: using setter function in cleanup process

My goal is to gain a deeper understanding of the React hooks life cycle, particularly focusing on minimizing re-rendering. To start off, I decided to create a simple component that displays the current time. The initial code snippet below functions perfect ...

Change to a dark theme using React hooks in typescript

Hello, I am new to React and English is not my first language, so please excuse any mistakes. I have been trying to enable a dark mode feature on my website. Most examples I have found involve toggling between dark and light modes where you need to specify ...

"Exploring the concept of odd and even numbers within the ng-repeat

Can anyone help me with detecting odd or even numbers in an ng-repeat? I have created a fiddle that displays some dots randomly showing and hiding. Now, I want to change the background color so that odd numbers are green and even numbers are red. function ...

Ways to resolve: The JSX component does not contain any construction or call signatures

I've been grappling with a persistent issue regarding the creation of custom elements dynamically in React TypeScript. If you're curious, you can check out the question here. const generalButtons: MenuButton[] = [ { text: "New Cl ...

Mobile devices cause CSS drop shadows to malfunction | Next.js

Update: After some testing, I discovered that this issue is specific to iPhones. When I tested it with an android device, everything worked perfectly fine. However, when I tried viewing the page on two different iPhones, it broke on both. This problem see ...

There was an issue with resolving the dependency for the required version of react in the package @ckeditor/[email protected] in the directory node_modules/@ckeditor/ckeditor5-react

click here for image description Dependency could not be resolved: npm ERR! peer react@"^16.13.1 || ^17.0.0" from @ckeditor/[email protected] npm ERR! node_modules/@ckeditor/ckeditor5-react npm ERR! @ckeditor/ckeditor5-react@"^3.0.3& ...

"React Spring's useTransition function unexpectedly displays the initial item thrice upon rendering

Encountering an issue with useTransition, where the console log displays transitions as Menu three times instead of Menu1, Menu2, and Menu3 Ttransition.map is also showing Menu1 repeated three times const [items, setItems] = useState([{ text: 'Menu1 ...