How to show images? It can not be shown correctly below.
In the src/components/Header.js
file:
import React from "react" import logo from "./logo.png" // Tell Webpack this JS file uses this image console.log(logo) // /logo.84287d09.png function Header() { // Import result is the URL of your image return } export default Header
The reason this is best is that it enables optimizations through the Webpack bundling pipeline, e.g. compression, data URLs, cache busting filename hashes, etc.
This is mostly useful for files other than images.
You can create a folder named
static
at the root of your project. Every file you put into that folder will be copied into thepublic
folder. E.g. if you add a file namedsun.jpg
to thestatic
folder, it’ll be copied topublic/sun.jpg
You can reference assets from the static folder in your code without anything special required:
render() { // Note: this is an escape hatch and should be used sparingly! // Normally we recommend using `import` for getting asset URLs // as described in the “Importing Assets Directly Into Files” page. return ; }
Corey's answer quotes the "Add custom webpack config" section of the Gatsby documentation, which is useful but unnecessary to load images.