19
15

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

React にて、publicフォルダ内の画像を参照する

19
Posted at

tl;dr

process.env.PUBLIC_URLもしくはwindow.location.originからpublicフォルダのパスを取得できます。

サンプル

🚀以下のサンプルは、create-react-app直後のApp.jsを編集したものです。

App.js
import React from 'react';

function App() {
  return (
    <div className="App">
        <img src={`${process.env.PUBLIC_URL}/logo192.png`} />
        <img src={`${window.location.origin}/logo192.png`} />
    </div>
  );
}

export default App;

参考

📖ReactJS and images in public folder

19
15
0

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
19
15

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?