Skip to content
GitLab
Projects Groups Snippets
  • /
  • Help
    • Help
    • Support
    • Community forum
    • Submit feedback
    • Contribute to GitLab
  • Sign in / Register
  • C create-react-app
  • Project information
    • Project information
    • Activity
    • Labels
    • Members
  • Repository
    • Repository
    • Files
    • Commits
    • Branches
    • Tags
    • Contributors
    • Graph
    • Compare
  • Issues 1,547
    • Issues 1,547
    • List
    • Boards
    • Service Desk
    • Milestones
  • Merge requests 417
    • Merge requests 417
  • CI/CD
    • CI/CD
    • Pipelines
    • Jobs
    • Schedules
  • Deployments
    • Deployments
    • Environments
    • Releases
  • Packages and registries
    • Packages and registries
    • Package Registry
    • Infrastructure Registry
  • Monitor
    • Monitor
    • Incidents
  • Analytics
    • Analytics
    • Value stream
    • CI/CD
    • Repository
  • Wiki
    • Wiki
  • Snippets
    • Snippets
  • Activity
  • Graph
  • Create a new issue
  • Jobs
  • Commits
  • Issue Boards
Collapse sidebar
  • Meta
  • create-react-app
  • Merge requests
  • !5457

Add forward ref to React SVG Component

  • Review changes

  • Download
  • Email patches
  • Plain diff
Merged Administrator requested to merge github/fork/appristas/master into master Oct 16, 2018
  • Overview 24
  • Commits 7
  • Pipelines 0
  • Changes 4

Created by: GasimGasimzada

This pull request adds "ref" option to SVGR loader; so that, the root svg component ref can be accessed. The way it works is that SVGR creates a forward ref and passed the ref to svg component using svgRef prop.

Example:

import React, { Component } from 'react';
import { ReactComponent as Logo } from './logo.svg'; // CRA default logo file

class App extends Component {
    myRef = React.createRef();

    componentDidMount() {
        console.log(this.myRef);
    }

    render() {
        return (<Logo svgRef={this.myRef} />);
     }
}

This example will print the svg element.

Testing

I have added unit tests to webpack/SVGComponent.js; however, I was not able to perform the test. yarn e2e:docker -- --test-suite kitchensin kept giving me permission denied error inside the container.

Assignee
Assign to
Reviewers
Request review from
Time tracking
Source branch: github/fork/appristas/master