i have objects with keys img and the value is relative path to image folder placed in src folder '/images'. I'm using map to loop over destructured data and adding it to DOM with simple template like this:
<div key={id}>
<img src={img + '.jpg'} alt='{title}' />
</div>
And my data object looks like this:
const category = [
{
id: 1,
title: 'cizme',
img: './images/cizme',
link: './cizme'},
...
data(category) is defined in App component
import data from './data';
import Home from './Home';
import logo from './images/logo_fin-01.png';
import Navbar from './Navbar';
function App() {
return (
<div className='App'>
<Navbar logo={logo} />
<Home data={data} />
</div>
);
}
export default App;
and Home comp is:
const Home = ({ data }) => {
return (
<div className='kat'>
{data.map(({ id, title, img, link }) => (
<li key={id}>
<span>{title}</span>
<div className='img-container'>
<img src={img + '.jpg'} alt={title}></img>
</div>
</li>
))}
</div>
);
};
export default Home;
Now, and in browser paths are displayed correctly, with no errors, but it says image 'could not load the image'.I tried to copy images folder to public but then i get error 'its out of scope or sth like this(i forgot)'.So what should i do?
update: require() works in sandbox, localy doesn't.
https://codesandbox.io/embed/zealous-christian-0j3v3?fontsize=14&hidenavigation=1&theme=dark