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

154
Visualizações
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 à 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