I want to include a image carousel in my Gatsby project. All images will be query from Contentful.
I used "react-responsive-carousel" and managed to make it work with simply import images directly. I am also certain that I can pull in the contentful images successfully without putting it into a carousel component.
Can anyone please help me with this? It seems gatsby-image component Img doesn't return a tag that "react-reponsive-carousel" can recognize. Or do you know any other carousel that will work?
My code:
import React, { Component, Fragment } from "react"
import "react-responsive-carousel/lib/styles/carousel.min.css"
import { Carousel } from "react-responsive-carousel"
import { graphql } from "gatsby"
import Img from "gatsby-image"
import tourimg from "../images/tour-1.jpg" //Import an image directly
export default class ApartmentPage extends Component {
render() {
const photos = this.props.data.allContentfulRooms.edges[0].node.photos
return (
<Carousel>
{photos.map(photo => {
const { fixed } = photo
return (
<div>
<Img fixed={fixed} />
{/* <img src={tourimg} /> */}
</div>
)
})}
</Carousel>
)
}
}
export const ROOM_QUERY = graphql`
{
allContentfulRooms {
edges {
node {
id
name
photos {
fixed(width: 150, height: 150) {
...GatsbyContentfulFixed_tracedSVG
}
}
}
}
}
}
`
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script>
Error message:
TypeError: Cannot read property 'map' of null
Thumbs.renderItems
node_modules/react-responsive-carousel/lib/components/Thumbs.js:174
171 | value: function renderItems() {
172 | var _this2 = this;
173 |
> 174 | return this.state.images.map(function (img, index) {
175 | var itemClass = _cssClasses2.default.ITEM(false, index === _this2.state.selectedItem && _this2.state.hasMount);
176 |
177 | var thumbProps = {