You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
Activity
GasimGasimzada commentedon Oct 16, 2018
I was able to figure out how SVGR works and how to add the feature to react script: #5457