Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

151
Vistas
React Template Literals don't work on SVG?

I have rendered an svg and morphed it using mouse position in vanilla javascript. Here is an example(make sure to refresh the page since it doesnt start automatically for some reason): https://codesandbox.io/s/svg-shape-change-mouse-position-sjl0z In vanilla js I did the following:

path.setAttribute("d", `M82 ${coords.y}L0 0H123V170H0L82 ${coords.y}Z`)

The problem: I can't implement the same logic in React. Whenever I add the template literals to the svg path it wont accept it. Here is my code: https://codesandbox.io/s/react-svg-shape-morph-mouse-position-r7mx9?file=/src/App.js

This react code gives me an error:

  let shape = useRef(null);
  console.log("thiss: " + shape.current);
  shape.current.setAttribute("d", `M82 ${y}L0 0H123V170H0L82 ${y}Z`);

  return (
    <div className="App">
      <svg viewBox="0 0 100% 100%" preserveAspectRatio="xMidYMid meet">
        <path ref={shape} d="M82 88L0 0H123V170H0L82 88Z" fill="lightblue" />
        {/* <path d="M82 88L0 0H123V170H0L82 ${y}Z" fill="#F9B300" /> */}
      </svg>

      <h1>{`x: ${x}; y: ${y};`}</h1>
    </div>
  );

How can I link the mouse position to the svg path shape? (ps. edited the question to include code snipets)

about 4 years ago · Juan Pablo Isaza
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda