score:90
You're using a relative url, which is relative to the current url, not the file system. You could resolve this by using absolute urls
<img src ="http://localhost:3000/details/img/myImage.png" />
But that's not great for when you deploy to www.my-domain.bike, or any other site. Better would be to use a url relative to the root directory of the site
<img src="/details/img/myImage.png" />
score:0
If your page url contains multiple / then in src
go back /
count minus 1 times.
For example page url http://localhost:3000/play/game/
then src
url must be ../your-image-folder/image-name
. And your your-image-folder
must be in public
folder.
score:0
I create my app with create-react-app and I use require instruction if I want to change dynamically my image src:
export const MyComponent = () => {
const [myImg, setMyImg] = useState(require('./path/to/my/img'));
const handleClick = () => {
setMyImg(require('./path/to/other/img'));
}
return (
<div>
<img src={myImg} alt='myImg' />
<button onClick={handleClick}>Click me!<button/>
<div>
)
}
score:1
Place the logo in your public folder under e.g. public/img/logo.png and then refer to the public folder as %PUBLIC_URL%:
<img src="%PUBLIC_URL%/img/logo.png"/>
The use of %PUBLIC_URL% in the above will be replaced with the URL of the public
folder during the build. Only files inside the public
folder can be referenced from the HTML.
Unlike "/img/logo.png" or "logo.png", "%PUBLIC_URL%/img/logo.png" will work correctly both with client-side routing and a non-root public URL. Learn how to configure a non-root public URL by running npm run build
.
score:2
A friend showed me how to do this as follows:
"./" works when the file requesting the image (e.g., "example.js") is on the same level within the folder tree structure as the folder "images".
score:2
I have used it this way and it worked perfectly
import Product from "../../images/product-icon.png";
import { Icon } from "@material-ui/core";
<Icon>
<img src={Product} style={{ width: "21px", height: "24px" }} />
</Icon>
score:3
Import Images in your component
import RecentProjectImage_3 from '../../asset/image/recent-projects/latest_news_3.jpg'
And call the image name on image src={RecentProjectImage_3} as a object
<Img src={RecentProjectImage_3} alt="" />
score:5
Adding file-loader npm to webpack.config.js per its official usage instruction like so:
config.module.rules.push(
{
test: /\.(png|jpg|gif)$/,
use: [
{
loader: 'file-loader',
options: {}
}
]
}
);
worked for me.
score:20
Some older answers din't work, others are good but won't explain the theme, in summary:
If image is in 'public' directory
Example: \public\charts\a.png
In html:
<img id="imglogo" src="/charts/logo.svg" />
In JavaScript:
Create image to new img, dynamically:
var img1 = document.createElement("img");
img1.src = 'charts/a.png';
Set image to existing img with id as 'img1', dynamically:
document.getElementById('img1').src = 'charts/a.png';
If image is in 'src' directory:
Example: \src\logo.svg
In JavaScript:
import logo from './logo.svg';
img1.src = logo;
In jsx:
<img src={logo} />
score:38
If the image is placed inside the 'src' folder, use the following:
<img src={require('../logo.png')} alt="logo" className="brand-logo"/>
score:39
Make an images folder inside src(/src/images) And keep your image in it. Then import this image in your component(use your relative path). Like below-
import imageSrc from './images/image-name.jpg';
And then in your component.
<img title="my-img" src={imageSrc} alt="my-img" />
Another way is to keep images in public folder and import them using relative path. For this make an image folder in public folder and keep your image in it. And then in your component use it like below.
<img title="my-img" src='/images/my-image.jpg' alt="my-img" />
Both method work but first one is recommended because its cleaner way and images are handled by webpack during build time.
score:65
With create-react-app
there is public folder (with index.html...).
If you place your "myImage.png" there, say under img sub-folder, then you can access them through:
<img src={window.location.origin + '/img/myImage.png'} />
score:186
If you used create-react-app to create your project then your public folder is accessible. So you need to add your image
folder inside the public folder.
public/images/
<img src="/images/logo.png" />
score:423
In create-react-app
relative paths for images don't seem to work. Instead, you can import an image:
import logo from './logo.png' // relative path to image
class Nav extends Component {
render() {
return (
<img src={logo} alt={"logo"}/>
)
}
}
Source: stackoverflow.com
Related Query
- Correct path for img on React.js
- react app doesn't read correct path for assets folder - local images don't show up on localhost, only shows an image icon
- Multiple path names for a same component in React Router
- What is the correct way of adding a dependency to react in your package.json for a react component
- Webpack 4 and react loadable does not seems to create correct chunk for server side rendering
- React TypeScript: Correct type for useLocation() from react-router-dom
- Typescript path mapping for React Native *.android.ts and *.ios.ts modules
- What is the correct type for React Click Event?
- Exclude a value for a path parameter in React Router by type
- what is the correct type for a React instance of a component in typescript
- React native Unable to use local tmp file path for Image.source iOS
- What is the correct typescript type for react children?
- React - correct way to wait for page load?
- correct naming specification for react components
- What is the correct pattern for utility function when using React hooks?
- React TypeScript | Correct Type for Event Handler Prop
- React TypeScript: Correct Types for onChange
- Correct handling of React Hooks for microphone audio
- How to enable VS code intellisense for absolute path imports in a React app?
- React TypeScript: Correct types for Route location and children properties
- How can I add multiple path for home page with react router dom 6?
- What's the correct way to do inheritance in TypeScript for React components?
- Is this a correct use case for the React useImperativeHandle hook?
- Correct Typescript types for Firebase in a React app
- Image path in css for react js project
- How to build programmatically the path for object data in React
- Add attributes to a path svg component for a map in React
- How to write right regular expression for my Route path in React JS
- Passing all http requests and React Routes through node backend first for session verification and then return path
- What is the correct type to use for this React form field hook?
More Query from same tag
- download folder from aws with express and React app
- How to get rejected promise from promise.all?
- React Router passing data from one component to another
- Webpack Error React & ES6 Previously working under Babelify
- React Javascript Handle Possible Null Data
- google-map-react custom skins
- Is it possible to do custom imports with Webpack like it is with Browserify?
- Express + React: CSRF Cookie is undefined in production, works locally
- React Redux - Showing Dialog after posting data not working - Unhandled Rejection (TypeError): Cannot read property 'openDialog' of undefined
- Apollo Client is not reading variables passed in using useQuery hook
- Error Element type is invalid: forgot to export your component from the file it's defined in, or you might have mixed up default and named imports
- State values resetting after onSubmit even if not successful
- react testing: shallow rendering - getRenderOutput returning null
- Action Creator and Reducer not console.logging, but the component is connected
- Next.js: How to use source-map-explorer with Next.js
- Unable to convert class component to function component
- How to retrieve selectedRows from a material-table in React
- How to draw dashed border style in react native
- Is there a way to get all this datas out and have them in an array
- Fetch and display image in reactjs from django backend using json
- Why do I have to wrap the functions of my onClick attributes in anonymous functions in React?
- Change css on click of a styled component (map function)
- Adding custom drodpdown on column Header in ag-Grid
- Typescript - pass new key in object as generic
- Argument of type is not assignable to parameter of type Typescript Error
- Docker-compose - node_modules not being populated at host
- API call in ReactJS and setting response to state
- Unable to use onClick event ReactJS
- React Antd Title - Bold
- reactjs - Is it possible to pass a property to a component as following?