Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SVGR enable forward ref for root node #5456

Closed
GasimGasimzada opened this issue Oct 16, 2018 · 1 comment
Closed

SVGR enable forward ref for root node #5456

GasimGasimzada opened this issue Oct 16, 2018 · 1 comment

Comments

@GasimGasimzada
Copy link
Contributor

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

Environment:
  OS:  macOS 10.14
  Node:  10.9.0
  Yarn:  1.9.4
  npm:  6.2.0
  Watchman:  4.7.0
  Xcode:  Xcode 10.0 Build version 10A255
  Android Studio:  Not Found

Packages: (wanted => installed)
  react: ^16.5.2 => 16.5.2
  react-dom: ^16.5.2 => 16.5.2
  react-scripts: 2.0.3 => 2.0.3

Steps to Reproduce

  1. Create ref in the component
  2. Pass ref to the SVG component

Expected Behavior

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:

animejs({
    targets: '.containerClassOfReactComponent path.line'
});

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.

@GasimGasimzada
Copy link
Contributor Author

I was able to figure out how SVGR works and how to add the feature to react script: #5457

@iansu iansu closed this as completed Mar 6, 2019
@lock lock bot locked and limited conversation to collaborators Mar 11, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants