Get the root node of svg file (i.e the svg element)
Actual Behavior
Warning: Stateless function components cannot be given refs. Attempts to access this ref will fail.
Reproducible Demo
import React, { Component } form 'react';
import { ReactComponent as WavyBackground } from './assets/wavy-bg.svg';
class App extends Component {
someRef = React.createRef();
componentDidMount() {
console.log(this.someRef);
}
render() {
return (<WavyBackground ref={this.someRef} />);
}
}
Additional Thoughts
At least for me, one of the main reasons for using SVG as inline/component rather than from a file is to be able to perform operations on it via CSS or JS. Most of the time it is about animations. Many animation libraries such as GSAP or AnimeJS require a DOM node or selector to perform the animations. Currently, the only way to pass objects is to pass a selector in the following way:
However, if SVGR --ref option is passed (at least in CLI), the generated SVG component's ref will be forwarded to the svg root element; therefore, we will be able to ref to the SVG component if we need to access the element.
Is this a bug report?
No
Did you try recovering your dependencies?
Yes
Which terms did you search for in User Guide?
Yes. SVG Ref; SVG forwardRef
Environment
Steps to Reproduce
Expected Behavior
Get the root node of svg file (i.e the
svg
element)Actual Behavior
Reproducible Demo
Additional Thoughts
At least for me, one of the main reasons for using SVG as inline/component rather than from a file is to be able to perform operations on it via CSS or JS. Most of the time it is about animations. Many animation libraries such as GSAP or AnimeJS require a DOM node or selector to perform the animations. Currently, the only way to pass objects is to pass a selector in the following way:
However, if SVGR
--ref
option is passed (at least in CLI), the generated SVG component'sref
will be forwarded to thesvg
root element; therefore, we will be able to ref to the SVG component if we need to access the element.