I am trying to import some local images into reactjs. But it's not working. I am using reactstrap
to make a carousel.
This is the error:
Module not found: Can't resolve '../../assets/img-3.jpg' in 'C:\Users\adity\Desktop\foodcubo-dev\project\src\Layouts\header'
I tried to import the images using the import
method, although the images are available in assets
. Import is fine, I guess. The problem might be related to rendering. I don't know.
This is my code:
Header.js
import React, { Component } from 'react';
import imgone from '../../assets/img-1.jpg'
import imgtwo from '../../assets/img-2.jpg'
import imgthree from '../../assets/img-3.jpg'
import {
Carousel,
CarouselItem,
CarouselIndicators,
CarouselCaption
} from 'reactstrap';
const items = [
{
src: {imgone},
altText: 'Slide 1',
caption: 'Slide 1'
},
{
src: {imgtwo},
altText: 'Slide 2',
caption: 'Slide 2'
},
{
src: {imgthree},
altText: 'Slide 3',
caption: 'Slide 3'
}
];
class Header extends Component {
constructor(props) {
super(props);
this.state = { activeIndex: 0 };
this.goToIndex = this.goToIndex.bind(this);
this.onExiting = this.onExiting.bind(this);
this.onExited = this.onExited.bind(this);
}
onExiting() {
this.animating = true;
}
onExited() {
this.animating = false;
}
goToIndex(newIndex) {
if (this.animating) return;
this.setState({ activeIndex: newIndex });
}
render() {
const { activeIndex } = this.state;
const slides = items.map((item) => {
return (
<CarouselItem
onExiting={this.onExiting}
onExited={this.onExited}
key={item.src}
>
<img src={item.src} alt={item.altText} />
<CarouselCaption captionText={item.caption} captionHeader={item.caption} />
</CarouselItem>
);
});
return (
<Carousel
activeIndex={activeIndex}
next={this.next}
previous={this.previous}
>
<CarouselIndicators items={items} activeIndex={activeIndex} onClickHandler={this.goToIndex} />
{slides}
</Carousel>
);
}
}
export default Header;
I did encounter the same problem and doing this fixed my problem:
./../../assets/img-3.jpg
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With