[RN] ๐ŸŒปsvg๋ฅผ ์ปดํฌ๋„ŒํŠธ๋กœ ์‚ฌ์šฉํ•˜๊ธฐ

TATAยท2023๋…„ 9์›” 5์ผ
0

React-Native

๋ชฉ๋ก ๋ณด๊ธฐ
6/7

โ’ˆ react-native-svg ์„ค์น˜

# ์„ค์น˜
npm install react-native-svg
# Expo์ผ ๊ฒฝ์šฐ โ†“
# expo install react-native-svg

npm install -D react-native-svg-transformer

โ’‰ metro.config.ts

metro.config.ts ํŒŒ์ผ ์ƒ์„ฑ

/* metro.config.ts */
const { getDefaultConfig } = require("expo/metro-config");

module.exports = (() => {
  const config = getDefaultConfig(__dirname);

  const { transformer, resolver } = config;

  config.transformer = {
    ...transformer,
    babelTransformerPath: require.resolve("react-native-svg-transformer"),
  };
  config.resolver = {
    ...resolver,
    assetExts: resolver.assetExts.filter((ext: string) => ext !== "svg"),
    sourceExts: [...resolver.sourceExts, "svg"],
  };

  return config;
})();

โ’Š svg ์ปดํฌ๋„ŒํŠธ

react-svgr์—์„œ ์‰ฝ๊ฒŒ svg ์ปดํฌ๋„ŒํŠธ๋ฅผ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋‹ค.
์‚ฌ์šฉํ•  svg ์ฝ”๋“œ๋ฅผ ๋ถ™์—ฌ๋„ฃ๊ธฐ ํ•˜๋ฉด ์ž๋™์œผ๋กœ ๋งŒ๋“ค์–ด์ค€๋‹ค.
(xmlns๋ถ€๋ถ„์€ ์‚ญ์ œ ํ›„ ์‚ฌ์šฉ)

์•„๋ž˜์ฒ˜๋Ÿผ ์‚ฌ์šฉ ๊ฐ€๋Šฅ

import Svg, { Path, Defs, LinearGradient, Stop } from 'react-native-svg';

interface IMusicBlueSvg {
  width: number;
  height: number;
}

const MusicBlueSvg = ({ width, height }: IMusicBlueSvg) => (
  <Svg width={width} height={height} fill="none">
    <Path
      fill="url(#a)"
      d="M20 1.27v14.807c0 .44-.148.833-.443 1.177-.295.343-.668.61-1.12.8-.45.189-.9.33-1.347.423a6.218 6.218 0 0 1-1.257.138c-.39 0-.81-.046-1.256-.138a7.312 7.312 0 0 1-1.348-.424 2.956 2.956 0 0 1-1.12-.8 1.755 1.755 0 0 1-.442-1.176c0-.44.147-.833.442-1.177.296-.344.669-.61 1.12-.8.452-.19.9-.33 1.348-.423a6.212 6.212 0 0 1 1.256-.138c.912 0 1.745.171 2.5.515v-7.1l-10 3.134v9.374c0 .44-.147.832-.442 1.176-.296.344-.669.61-1.12.8-.452.19-.9.33-1.348.423A6.22 6.22 0 0 1 4.167 22c-.391 0-.81-.046-1.257-.139a7.311 7.311 0 0 1-1.348-.423 2.957 2.957 0 0 1-1.12-.8A1.756 1.756 0 0 1 0 19.461c0-.44.148-.832.443-1.176.295-.344.668-.61 1.12-.8.45-.19.9-.33 1.347-.423a6.219 6.219 0 0 1 1.257-.139c.911 0 1.744.172 2.5.516V4.654c0-.273.082-.522.247-.747.165-.225.378-.381.638-.47L18.385.054C18.49.018 18.611 0 18.75 0c.347 0 .642.123.885.37s.365.547.365.9Z"
    />
    <Defs>
      <LinearGradient id="a" x1={10} x2={10} y1={0} y2={22} gradientUnits="userSpaceOnUse">
        <Stop stopColor="#7FB7BE" />
        <Stop offset={0.495} stopColor="#51858C" />
        <Stop offset={1} stopColor="#256772" />
      </LinearGradient>
    </Defs>
  </Svg>
);
export default MusicBlueSvg;

โž• ์ด๋ฏธ์ง€ ๊ฐ€์ ธ์˜ค๊ธฐ

require('๊ฒฝ๋กœ')๋กœ ์ด๋ฏธ์ง€๋ฅผ ๊ฐ€์ ธ์˜ฌ ์ˆ˜๋„ ์žˆ์ง€๋งŒ

<Image source={require('./src/assets/png-file/crayon-line.png')} />

์•„๋ž˜์ฒ˜๋Ÿผ ์‚ฌ์šฉํ•˜๊ณ  ์‹ถ๋‹ค๋ฉด

import crayonPng from './src/assets/png-file/crayon-line.png';

<Image source={crayonPng} />

import-image.d.ts ํŒŒ์ผ์„ ์ƒ์„ฑํ•˜๊ณ ,
์ด๋ฏธ์ง€ ํŒŒ์ผ ํ™•์žฅ์ž์ธ .png ๋ฐ .jpg์— ๋Œ€ํ•œ ๋ชจ๋“ˆ ํƒ€์ž…์„ ์„ ์–ธํ•˜๋ฉด ๋œ๋‹ค.

/* import-image.d.ts */
declare module '*.png';
declare module '*.jpg';
profile
๐Ÿพ

0๊ฐœ์˜ ๋Œ“๊ธ€