By default, Re.Pack's Assets loader is configured to allow you to import SVGs in you code, but that doesn't mean you can render them immediately.
To render SVGs in your application, you first need to add react-native-svg
native module to your application.
Please follow this installation instructions here: https://github.com/react-native-svg/react-native-svg#installation
Now that you have react-native-svg
installed and linked into the application, you need to tell Webpack not to use Re.Pack's Assets loader from processing SVGs, since you will process them manually later.
Go to your Webpack configuration and apply the following diff:
If you don't have Re.Pack's Assets loader rule in your Webpack config, read this guide first.
Now you need to tell Webpack how to handle .svg
files.
@svgr/webpack
Using SVGR is a good option, if you want to render imported SVGs in the same way as you would render any other React component:
To use SVGR, you need to add an additional rule to process SVGs with @svgr/webpack
loader.
Go to your Webpack configuration and apply the following diff:
Thanks to the rule above, your SVGs will be processed by @svgr/webpack
and converted to components
from react-native-svg
allowing you to simply
import the component and render it as a React component.
Using Webpack's asset modules is a simpler and faster way to render SVGs in your application.
It's a good option, if you don't need or care about using the imported SVG as React component,
and you're fine with using SvgXml
or SvgUri
component from react-native-svg
.
You can read more about Webpack's asset modules here: https://webpack.js.org/guides/asset-modules/.
SvgXml
+ asset/source
The SVG file will be included in a bundle as a raw source in a separate module.
SvgUri
+ asset/inline
The SVG file will be included in a bundle as a data URI in a separate module.
To use asset modules, you need to add additional rule to process SVGs as an asset module.
Go to your Webpack configuration and apply the following diff:
Now you can import the XML of your SVG in you code and render it using SvgXml
from react-native-svg
or SvgUri
if you chose asset/inline
type.
If you want to inline SVGs as data URI, use asset/inline
in the rule:
Remember to use SvgUri
for inlined SVGs instead of SvgXml
.
You can also use both, asset/source
and asset/inline
in your application:
When using both assets/source
and assets/inline
, you can specify different test
, include
and exclude
values,
so that Webpack can figure out which mechanism to use for which SVG. You can read more about rule conditions here:
Do not use asset/resource
type, as it won't work correctly in React Native and can potentially
crash your application.