๋ฆฌ์กํธ๋ฅผ ์ฌ์ฉํ๋ค๊ฐ ์ ๋ง ๋ฌ๊ธ์๋ ์๋ฌ๊ฐ ๋ฐ์ํ์๋ค.
์ด์ ๊น์ง ์ ์ฝ๊ณ ์์์์. ์ ๊ฐ์๊ธฐ ์๋ค๋ ๊ฑฐ์ฌ? ์์ธ์ ์์ง๊น์ง ๋ชจ๋ฅด๊ฒ ์ง๋ง ํด๊ฒฐ์ฑ ์ ์ฐพ์๋ค.
src ์์ custom.d.ts
๋ฅผ ๋ง๋ ๋ค. (์๋ฐ์คํฌ๋ฆฝํธ ํ๋ก์ ํธ๋ผ๋ฉด .js๋ก ๋ง๋ ๋ค)
declare module '*.svg' {
const content: any;
export default content;
}
declare module "*.png" {
const value: any;
export = value;
}
์ถ๊ฐํ๋ฉด ์ ๋๋ก ์ธ์๋๋ค.
๋ฌธ์ ๋ svg ์ฌ์ฉํ๋ ๋ฐฉ์์ด ๋ฐ๋๋ค๋ ๊ฒ์ด๋ค. ์๋๋ {ReactComponent as Siren}
์ด๋ ๊ฒ ์์ฑํ๋๋ฐ ์ ํ์ผ์ ์ถ๊ฐํ ๋ค๋ก ์ปดํฌ๋ํธ๋ฅผ ์ธ์ํ์ง ๋ชปํ๋ค.
import Siren from "../../assets/images/button/siren.svg";
const StyledSiren = styled.button`
width: 24px;
height: 24px;
background-image: url(${Siren});
background-size: contain;
background-repeat: no-repeat;
`;
background-image url์ด๋ img content๋ก ์ค์ ํ์ฌ ์ฌ์ฉํ๋ค.
๋ง์ฝ ์ปดํฌ๋ํธ๋ก ์ฌ์ฉํ๊ณ ์ถ๋ค๋ฉด ๋ค์๊ณผ ๊ฐ์ด ๋ณ๊ฒฝํ๋ค.
declare module '*.svg' {
import { ReactElement, SVGProps } from "react";
const content: (props: SVGProps<SVGElement>) => ReactElement;
export default content;
}
๐จ Uncaught DOMException: Failed to execute 'createElement' on 'Document': The tag name provided
์ด๋ ๊ฒ ํ๋ฉด ๋ ์๋ฌ๊ฐ ๋ ๊ฒ์ด๋ค. ์ด๋ด ๋๋ svg๋ฅผ ์ปดํฌ๋ํธ๋ก ๋ง๋ค์ด์ ์ฌ์ฉํ๋ฉด ๋๋ค.
import React from "react";
const Logo = props =>
<svg >
.... svg code
</svg>;
export default Logo;
์ด๊ฒ ๊ท์ฐฎ์ผ๋ฉด svgr์ ์ฌ์ฉํ๋ฉด ๋๋ค.