Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get image from public folder without using Webpack

I have read a lot of posts on getting image from the public folder with Webpack. However, is there anyway to do so without a Webpack?

My code structure:

./public/logo.png
./src/../source_file.js (which renders an <img> and that needs to be pointed to the logo image

I tried import img from './logo.png' but that is not working. Also tried relative path but reactjs is preventing me from importing anything outside src folder.

like image 890
Mr.cysl Avatar asked Oct 25 '25 23:10

Mr.cysl


1 Answers

As long as your package.json is at the root, this will work:

import logo from '../../public/logo.png';

const MyComponent = () => (<div>
 <img src={logo} alt={"logo"} /> 
</div>);
export default MyComponent;

Otherwise, assuming you are copying your /public folder to the root of your webserver, you can also simply do <img src="/logo.png" alt="logo">

like image 74
AnilRedshift Avatar answered Oct 28 '25 15:10

AnilRedshift



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!