Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

159
Views
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
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!