If you use the create-react-app workflow, put the assets in a shared folder and use the special variable PUBLIC_URL.
Inside index.html use% PUBLIC_URL%:
<link rel="shortcut icon" href="%PUBLIC_URL%/favicon.ico">
Inside JS / JSX files, use process.env.PUBLIC_URL:
render() {
// Note: this is an escape hatch and should be used sparingly!
// Normally we recommend using import for getting asset URLs
// as described in "Adding Images and Fonts" above this section.
return <img src={process.env.PUBLIC_URL + '/img/logo.png'} />;
}
Recommended approach :
import stylesheets, images and fonts from JavaScript, placing them together with src files.
import React from 'react';
import logo from './logo.png';
console.log(logo);
function Header() {
return <img src={logo} alt="Logo" />;
}
export default Header;
Adding assets to a shared folder
When to use a public folder
source
share