Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

229
Visualizações
Set React Color Value to Certain Parts of SVG

Is it possible to set my dynamic primary color from ReactJS unto a specific class of the SVG? If so, how do I implement it?

Error.svg

<!-- Generator: Adobe Illustrator 26.0.0, SVG Export Plug-In . SVG Version: 6.00 Build 0)  -->
<svg version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px"
     viewBox="0 0 500 500" style="enable-background:new 0 0 500 500;" xml:space="preserve">
<style type="text/css">
    ....
    .st9{fill:#E0E0E0;}
    .st10{display:inline;}

    <!-- Primary -->
    .st11{fill:#CA0B00;} <--- Here is the color that I want to be able to change dynamically
    <!-- Primary -->

    .st12{opacity:0.9;fill:#FFFFFF;}
    .st13{fill:#263238;}
    ....
</style>
....
</svg>

App.js

import React, {useEffect, useState} from "react";
import Error from "./Error.svg";

const App = () => {
   const [primary, setPrimary] = useState("#FFFFFF");

   useEffect(() => {....},[]); <--- sets the color for the primary 

   return <img src={Error} />
}

export default App;
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

You can use a tool like SVGR to convert the SVG into a React Component. Then you can pass your dynamic color in as a prop to your SVG component and assign it to the fill attribute on whatever SVG element you wish.

Depending on the desired conditions for changing your color, your final solution may differ. However, the working example below is a simplified version of the sample code you provided.

You can use the Re-render button in this example to see the effect in action. It triggers an onClick handler that sets the state - primary - with a random color. We include primary in the dependency array in useEffect so that it performs a re-render any time our state is updated.

import React, { useState, useEffect } from "react";

const SvgComponent = (props) => (
  <svg xmlns="http://www.w3.org/2000/svg" height={140} width={500}>
    <path className="my-target-class" fill={props.color} d="M0 0h300v100H0z" />
  </svg>
)

function App() {
  const colors = ["#7FFF00", "#ADFF2F", "#FF0000", "#00FF7F", "#00FF7F"];
  const [primary, setPrimary] = useState("#7FFF00");

  useEffect(() => {
    console.log("state changed");
    console.log("color is: ", primary);
  }, [primary]);

  const handleClick = () => {
    const color = colors[ Math.floor(Math.random() * colors.length) ];
    setPrimary(color);
  }

  return (
    <div>
      <button onClick={(handleClick)}>Re-render</button>
      <SvgComponent color={primary} />
    </div>
  );
}

export default App;
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda