React-Native-SVG is a convenient library for developing applications with scalable vector graphics (SVG). It combines the power of react-native and SVG to help developers create an interactive experience. It can be used for 2D graphics and animations, with the help of JavaScript code. This library makes it easier for developers to design, animate and control SVG elements within the applications. With React-Native-SVG, developers can bring out the same rich experience to their react-native applications as compared to other platforms.
Installing React-Native-SVG
To install React-Native-SVG, you need to add the following lines to the package.json in your project:
"dependencies": {
" react-native-svg": "7.1.1"
}
After that, run the following command to install the React-Native-SVG library:
$ npm install
Setting Up SVG View
To set up the SVG view, you need to include the mentioned lines in your application's setup file:
import { SVGRender } from 'react-native-svg';
<SVGRender
src=""
height=""
width=""
onLoaded={(shape) => {
console.log(shape);
}} />
The src property will take the tag which contains the SVG elements. It requires a path which leads to a file that includes the svg document. The width and height properties are used to specify the size of the SVG. The onLoaded (shape) will help to access the elements of the svg document.
Animating SVG View
The most powerful feature of React-Native-SVG is the animation capability. To animate an SVG view, you need to use the SVGAnimated instance. For example, if you want to animate the height, use
let { height } = this.state;
let animatedHeight = new SVGAnimated( height );
animatedHeight.addListener(({ value }) => {
this.setState({ height: value });
});
animatedHeight.animate( [
{ toValue: 100 },
{ toValue: 0 },
], {
duration: 500
});
Similarly, you can animate other properties such as width, stroke, and opacity, etc.
Conclusion
React-Native-SVG is a powerful library which brings a whole set of new possibilities to your react-native application. With its simple to use API and animation support, developers can quickly get started and build amazing applications.
FAQ
Q. What is React-Native-SVG?
A. React-Native-SVG is a library for developing applications with scalable vector graphics (SVG). It combines the power of react-native and SVG to help developers create an interactive experience.
Q. How to install React-Native-SVG?
A. To install React-Native-SVG, you need to add the library to the dependencies in the package.json file and then run the npm install
command.
Q. How to animate SVG view using React-Native-SVG?
A. To animate the SVG view, you need to use the SVGAnimated instance. For example, let animatedHeight = new SVGAnimated(height);
. For further information, please refer to the guide above.
Q. Does React-Native-SVG provide any advantages over other libraries?
A. Yes. React-Native-SVG provides developers a whole set of new possibilities to build react-native applications. With its simple to use API and animation support, developers can quickly get started and build amazing applications.
Q. Does React-Native-SVG requires any special skill to use?
A. No. React-Native-SVG is easy to use for anybody with basic knowledge of JavaScript. It does not require prior experience in react-native or SVG.