Uh oh!
There was an error while loading. Please reload this page.
Add forward ref to React SVG Component - #5457
Conversation
facebook-github-bot
commented
Oct 16, 2018
Thank you for your pull request and welcome to our community. We require contributors to sign our Contributor License Agreement, and we don't seem to have you on file. In order for us to review and merge your code, please sign up at https://code.facebook.com/cla. If you are contributing on behalf of someone else (eg your employer), the individual CLA may not be sufficient and your employer may need the corporate CLA signed. If you have received this in error or have any questions, please contact us at cla@fb.com. Thanks! |
iansu
commented
Oct 17, 2018
This looks good. I think we just need to get the tests passing. |
GasimGasimzada
commented
Oct 17, 2018
Okay. Then, I’ll try to figure out how to perform the tests and fix if there are any problems. |
GasimGasimzada
commented
Oct 17, 2018
There was a need to update Jest file transform for SVG to include ref via svgRef prop. Also some additional fixes were necessary. I still was not able to get |
gaearon
left a comment
There was a problem hiding this comment.
We shouldn't need a separate prop for this. Instead there should be a way to tell svgr to use React.forwardRef which does the same thing directly for the ref attribute.
Adding “ref” option to SVGR webpack config does add forwardRef to the SVG component. However, the prop that is being used to pass ref is |
gaearon
commented
Oct 17, 2018
I don't quite understand what it means. The sole purpose of |
I think |
GasimGasimzada
commented
Oct 17, 2018
To be completely honest, I dont quiet understand it either. I tried to use I might be missing something here and I’ll investigate to see what the issue might be. |
gaearon
commented
Oct 17, 2018
Where does svgr use |
GasimGasimzada
commented
Oct 17, 2018
This is the template directory for SVGR Transform: https://github.com/smooth-code/svgr/tree/master/packages/core/src/templates util.js is where forwardRef is implemented + the reactDOMTemplate.js for the main template. |
Apparently it was added specifically for a library called @neoziro Can we make the solution generic so it works with ReactDOM? Ideally it's |
gregberge
commented
Oct 18, 2018
@gaearon the latest version of SVGR supports You can try it here in https://svgr.now.sh/. |
gregberge
commented
Oct 18, 2018
@gaearon if you want to upgrade SVGR, you should probably wait for the next version. I will change a lot of things:
So it will result by a huge performance improvement and a smaller package size. I plan to release it in one or two weeks. |
GasimGasimzada
commented
Oct 18, 2018
I completely forgot about checking what version the ref was added to SVGR and got confused why ref was not working :/ Upgrading locally to v3.1.0 worked fine with. I modified the test files to use refs instead of svgRef and added forward ref to the SVG transform for Jest. However, the packages are not upgraded; so, the tests are failing (locally everything works as expected). |
iansu
commented
Oct 18, 2018
@neoziro This is good to know. I was considering updating CRA to use SVGR 3 but now I'll wait for the new version to be released. Thanks! |
This pull request has been automatically marked as stale because it has not had any recent activity. It will be closed in 5 days if no further activity occurs. |
gregberge
commented
Nov 18, 2018
@iansu any update? |
iansu
commented
Nov 19, 2018
There's a PR to upgrade to SVGR 4: #5816. I think it makes sense to merge that first and then update and merge this branch. |
GasimGasimzada
commented
Feb 13, 2019
@iansu Any update on this? |
iansu
commented
Feb 13, 2019
@GasimGasimzada Now that SVGR has been upgraded you should be able to update this branch. A few things have changed, for example our webpack config is now in a single file instead of separate dev and prod files. Once your branch is updated and everything is working we should be able to merge this. |
GasimGasimzada
commented
Feb 13, 2019
@iansu Great! I will make my changes by tomorrow then :) |
GasimGasimzada
commented
Feb 13, 2019
I have updated SVGR configuration in the new webpack config. |
iansu
commented
Feb 14, 2019
Thanks! |
This pull request adds "ref" option to SVGR loader; so that, the root
svgcomponent ref can be accessed. The way it works is that SVGR creates a forward ref and passed the ref to svg component usingsvgRefprop.Example:
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 kitchensinkept giving me permission denied error inside the container.