How to send Gatsby-Image to component as property

Viewed 1284

I've been trying to figure out how to use Gatsby-image with components and I just can't seem to figure it out.

I'm using Gatsby-image, gatsby-transformer-sharp, gatsby-plugin-sharp.

Here's my query:

export const pageQuery = graphql`
  query {
    imageOne: file(relativePath: { eq: "tbbs1.jpg" }) {
      childImageSharp {
        fluid(maxWidth: 1000) {
          ...GatsbyImageSharpFluid
        }
      }
    }
  }
`

and using it like this works fine as many tutorials show:

<Img fluid={props.data.imageOne.childImageSharp.fluid} />

Here is the component

import React, { Component } from "react";
import Slider from "react-slick";
import Img from "gatsby-image";


export default class SimpleSlider extends Component {
  constructor(props) {
    super(props);
    this.next = this.next.bind(this);
  }
  next() {
    this.slider.slickNext();
  }
  render() {
    const settings = {
      dots: true,
      lazyLoad: 'progressive',
      arrows: false,
      infinite: true,
      speed: 500,
      slidesToShow: 1,
      slidesToScroll: 1
    };

    let slides = this.props.bilder.map( (item, index) =>
      (
        <div key={index} className="slide-container">
          <div className="slider-box" onClick={this.next}>
            <img src={this.props.bilder[index]} alt=""/>
          </div>
        </div>
      )
    );

    return (
      <div id="work" className="slick-container">
        <div className="title-desc-container">
          <h3>{this.props.title}</h3>
          <p>{this.props.desc}</p>
        </div>
        <Slider ref={c => (this.slider = c)} {...settings}>
          {slides}
        </Slider>
      </div>
    );
  }
}

but what I'd like to do is something like this:

<SimpleSlider title="title" desc="description" images={[props.data.imageOne.childImageSharp.fluid, props.data.imageTwo.childImageSharp.fluid]} />

I'm guessing there's an easy solution for this, but I haven't found it yet.

Any help is greatly appreciated!

Thanks!

1 Answers

There are two mistakes in your code.

  1. You are using the HTML tag <img />. You have to use <Img /> component from gatsby-image to make it work (you are importing it, but you are not using it).
  2. You are not passing any bilder prop to your component but you are trying to call it in your SimpleSlider component. Just map your images props and it's enough. (Or pass the desired bilder prop)

Refactor your SimpleSlider as follow:

import React, { Component } from "react";
import Slider from "react-slick";
import Img from "gatsby-image";


export default class SimpleSlider extends Component {
  constructor(props) {
    super(props);
    this.next = this.next.bind(this);
  }
  next() {
    this.slider.slickNext();
  }
  render() {
    const settings = {
      dots: true,
      lazyLoad: 'progressive',
      arrows: false,
      infinite: true,
      speed: 500,
      slidesToShow: 1,
      slidesToScroll: 1
    };

    return (
      <div id="work" className="slick-container">
        <div className="title-desc-container">
          <h3>{this.props.title}</h3>
          <p>{this.props.desc}</p>
        </div>
        <Slider ref={c => (this.slider = c)} {...settings}>
          {this.props.images.map((image, index) => (
            <div key={index} className="slide-container">
              <div className="slider-box" onClick={this.next}>
                <Img src={image} style={{width: 200, height: 200}}/>
              </div>
            </div>
          ))}
        </Slider>
      </div>
    );
  }
}

Related