LoginSignup
17
6

More than 3 years have passed since last update.

Next.jsでsvgを使用する

Last updated at Posted at 2020-04-01

概要

  • TypeScript × Next.jsでsvgを使う

手順

Next.jsのリポジトリにsvgを使用する例があります。

これを参考にbabel-plugin-inline-react-svgを追加

yarn add -D babel-plugin-inline-react-svg

.babelrc に以下を追記

.babelrc
{
  "presets": [ "next/babel" ],
  "plugins": [ "inline-react-svg" ]
}

これでNext.js上で、svgをそのままインポートして利用できます。

しかし、Typescriptを使用している場合、次のようなエラーが出ます。

Cannot find module './logo.svg'.ts

これは解決するには以下の宣言を追加します。

index.d.ts
declare module '*.svg'

以上で問題なくsvgが使用できるはずです。

17
6
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
17
6