최초 작성일 : 2024.11.22
최종 수정일 : 2024.11.22
1. 개요
서비스를 만들다 보면 SVG파일을 사용해야 할 경우가 잦다.
SVG파일을 이용하다보면 코드로 간편하게 사용하고싶을 때가 있는데, 이 때 사용할 수 있는 라이브러리를 소개한다!
개인적으로도 거의 필수로 이용하고 있는 라이브러리이다.
2. react-native-svg
react-native-svg라이브러리는 React Native에서 SVG파일을 이용할 수 있게 해주는 라이브러리이다.
https://github.com/software-mansion/react-native-svg
GitHub - software-mansion/react-native-svg: SVG library for React Native, React Native Web, and plain React web projects.
SVG library for React Native, React Native Web, and plain React web projects. - software-mansion/react-native-svg
github.com
npm install을 이용해 간단하게 설치할 수 있다.
npm install react-native-svg
cd ios && pod install
3. react-native-svg-transformer
react-native-svg-transformer라이브러리는 SVG파일을 수정할 수 있게 해주는 라이브러리이다.
react-native-svg라이브러리와 함께 사용된다.
https://github.com/kristerkari/react-native-svg-transformer
GitHub - kristerkari/react-native-svg-transformer: Import SVG files in your React Native project the same way that you would in
Import SVG files in your React Native project the same way that you would in a Web application. - kristerkari/react-native-svg-transformer
github.com
npm install과 추가 작업이 필요하다.
npm install --save-dev react-native-svg-transformer
cd ios && pod install
프로젝트 루트 폴더에 metro.config.js파일을 생성한 뒤 아래의 내용을 작성한다.
const { getDefaultConfig, mergeConfig } = require("@react-native/metro-config");
const defaultConfig = getDefaultConfig(__dirname);
const { assetExts, sourceExts } = defaultConfig.resolver;
/**
* Metro configuration
* https://reactnative.dev/docs/metro
*
* @type {import('metro-config').MetroConfig}
*/
const config = {
transformer: {
babelTransformerPath: require.resolve(
"react-native-svg-transformer/react-native"
)
},
resolver: {
assetExts: assetExts.filter((ext) => ext !== "svg"),
sourceExts: [...sourceExts, "svg"]
}
};
module.exports = mergeConfig(defaultConfig, config);
Typescript를 이용한다면 프로젝트 루트 폴더에 declarations.d.ts파일을 만들고 아래의 내용을 작성한다.
declare module "*.svg" {
import React from "react";
import { SvgProps } from "react-native-svg";
const content: React.FC<SvgProps>;
export default content;
}
4. SVG변경하기
위의 세팅을 완료했다면 이제 SVG파일을 import한 뒤 width, height를 이용해 크기, fill을 이용해 색을 변경할 수 있다.
import Logo from "./logo.svg";
export default function Screen() {
return (
<Logo width={120} height={40} fill = {"#f00"} />
)
}
'프론트엔드' 카테고리의 다른 글
[Vercel] Vercel CLI로 프로젝트 배포하기 (0) | 2024.11.24 |
---|---|
[React Native] React Native는 더이상 bridge방식을 이용하지 않는다. (2) | 2024.11.23 |
[Typescript] Enum보단 Literal Types를 사용하자! (0) | 2024.11.21 |
[Javascript, Tip] 효율적으로 객체 복사하기 structuredClone() (1) | 2024.11.20 |
[React, 짧은글] VirtualDOM을 이용하면 속도가 빨라질까? (0) | 2024.11.19 |