I'm making a small react application to traverse all images present in my directory. I want to change image when button is clicked. The problem is image won't load on the web.
When I'm using import method to import images then it is working fine, but I think this approach is not good when I have multiple images to work with.
I also used require() but even in this case image won't load. I don't know what's the problem.
import React from 'react'
const MyCollection = [
{
id:0,
path:"./images/0.jpg",
},
{
id:1,
path:"./images/1.jpg",
},
{
id:2,
path:"./images/2.jpg",
},
{
id:3,
path:"./images/3.jpg",
},
];
export default function App() {
const [index, setActiveStep] = React.useState(0);
const goToNextPicture = () => {
setActiveStep((prevActiveStep) => prevActiveStep + 1);
};
return (
<div>
<img src={MyCollection[index].path} height={200} width={200} alt=""/>
<br/>
<input type="button" onClick={goToNextPicture} value="Next"/>
</div>
)
}
Thank You!