I’ve got a problem with showing image from js file. I really dont know what i can do with that. Can someone help me?
Code for main component:
import React from 'react'
import {jetData} from '../jetData'
const MainBar = () => {
return (
<div className='text-white'>
{jetData.map((data, key) => {
return (
<div key={key}>
{<img src={data.image} /> + data.name}
</div>
);
})}
</div>
)
}
export default MainBar
Code for js file with data:
import f18 from './assets/jets/f-18cd-hornet.png'
import f22 from './assets/jets/f-22-raptor.png'
import mig29 from './assets/jets/mig-29.png'
import su27 from './assets/jets/Sukhoi_Su-27SKM.png'
export const jetData = [
{
image: {f18},
name: 'F/A-18 Hornet'
},
{
image: {f22},
name: 'F-22 Raptor'
},
{
image: {mig29},
name: 'MiG-29'
},
{
image: {su27},
name: 'Su-27'
},
]
3
Answers
Check if this works i hope it will work sorry at this point i can not run it for you
I think what you are trying to do is this:
Which works fine with .svg files, but not with .jpg and .png. The reason seems to be that
import logo from './logo.svg';
stores the path in the variable, butimport f18 from './assets/jets/f-18cd-hornet.png
stores a, what to me looks like, a string representation of the image.I think this is the way to go:
Your imports do not need to be in curly braces:
Your map key will likely throw an error because you are using the index as a key. I would change it to the following