I’m making a React application involving SVG’s which react to mouseclicks. Every time the user clicks on the screen, the SVG will morph and point to the click position.
The SVG also has an tag within it which is supposed to excecute an animation 1 time.
Ideally, this animations should execute once every time the mouse is clicked. However I am finding that it only executes on the first mouse click. The subsequent clicks do not have an effect.
I am guessing this is happening because the tag is rendered once onto the DOM and it just stays there.
Ideally I would like it to destroy that element, and render a fresh one each time the mouse is clicked.
How do you do this in React?
Here is an outline of my code.
function Drawing(inp){
var [startPosLocal, set_startPosLocal] = useState(inp.startPos);
var [destPosLocal, set_destPosLocal] = useState(inp.destPos);
let pathString = createRayPathString(inp.startPoint, inp.endPoint);
return(
<g>
<path d={pathString} stroke="blue" stroke-width="2" >
{/*This is the element that I want destroyed and rendered on each click*/}
<animate dur="1s" values"0; 1;" repeatCount="1" />
</path>
</g>
)
}
Note that I have created 2 state variables to represent the mouse start and destination positions however they are not currently in use. I have tried inputting these state variables into the animate tag (thinking that state variables will trigger re-renders) but it didn’t work.
2
Answers
You can try removing the
animate
element (or theg
element if it does not work) for a render and adding it again.…
or
I’ve tried a similar solution suggested by palindrom but i added a key prop to my svg and it’s working.
)
}