使用带有graphql查询功能的React-slick的问题

时间:2019-03-19 17:32:11

标签: javascript reactjs graphql react-slick

我正在尝试使用react-slick在项目中显示多个项目的轮播,但是在映射graphql查询中的数据时遇到问题。只有当我手动放置物品时,圆盘传送带才能很好地工作……但想法并非如此。

然后,我放上运行良好的代码:

------ SliderCarousel.js ------

  import React from 'react'

  import Slider from "react-slick"

  import { Grid } from '@material-ui/core';

  /** @jsx jsx */
  import { jsx, css } from '@emotion/core'

  const borde = css({
  borderStyle: 'solid',
  })

  const contenedorCarousel = css({

  padding:30,
  backgroundColor: '#39854A'
  })
  const itemCarousel = css({
 })

  const itemBody = css({
  marginLeft: 5,
  height: 136,
  width: 136,
  backgroundColor: 'white',
  borderRadius: 12
  })

  const titulo = css({
  paddingTop: 10
  })
  const SliderCarousel  = props =>{

  const settings = {
        dots: false,
        infinite: true,
        speed: 1100,
        slidesToShow: 6,
        slidesToScroll: 3,
        autoplay: true,
        autoplaySpeed: 8000
      }
  return (
        <div css={contenedorCarousel}>
              <Slider {...settings}>
                     <div css={itemCarousel}><div css={itemBody}>
                          <Grid container
                          direction="column"
                          justify="center"
                          alignItems="center">
                                <Grid item css={titulo}>1</Grid>
                                <Grid item css={[{
                                backgroundImage:  
                           `url('${require('../../images/productos/
                            5c89b2b19268c22ab85a24c5.png')}')`,
                                backgroundSize: 'contain',
                                backgroundRepeat: 'no-repeat',
                                height: 90,
                                width: 90,}]}></Grid>
                          </Grid>
                          </div></div>
                          <div css={itemCarousel}><div css={itemBody}>
                          <Grid container
                          direction="column"
                          justify="center"
                          alignItems="center">
                                <Grid item css={titulo}>2</Grid>
                                <Grid item css={[{
                                backgroundImage:  
                               `url('${require('../../images/productos/
                                5c89b2b19268c22ab85a24c5.png')}')`,
                                backgroundSize: 'contain',
                                backgroundRepeat: 'no-repeat',
                                height: 90,
                                width: 90,}]}></Grid>
                          </Grid>
                    </div></div>
                    </Slider>
                   </div>

                    )


                    }
                   export default SliderCarousel

----下面的代码显示了水平方向的项目,并且无法直观地工作。我认为渲染通过graphql的元素时会遇到问题。

import React from 'react'
import ItemCarrousel from '../Productos/ItemCarrousel'
import Slider from "react-slick"
import { Query } from "react-apollo"
import gql from "graphql-tag";
import { Grid } from '@material-ui/core';

/** @jsx jsx */
import { jsx, css } from '@emotion/core'

 const borde = css({
  borderStyle: 'solid',
 })

const contenedorCarousel = css({

  padding:30,
  backgroundColor: '#39854A'
})
const itemCarousel = css({


 })

  const itemBody = css({
  marginLeft: 5,
  height: 136,
  width: 136,
  backgroundColor: 'white',
  borderRadius: 12
 })

  const titulo = css({
  paddingTop: 10
  })


   export const PRODUCTOS = gql`
   query productos{
    products{
    _id
    title
   price

   }
  }

 `;
 const SliderCarousel  = props =>{

  const settings = {
        dots: false,
        infinite: true,
        speed: 1100,
        slidesToShow: 6,
        slidesToScroll: 3,
        autoplay: true,
        autoplaySpeed: 8000
      }
  return (
        <div css={contenedorCarousel}>
              <Slider {...settings}>

                    <Query query={PRODUCTOS}>
                    {({loading,error,data})=>{
                          if (loading) return "Loading...."
                          if (error) return `Error!: ${error}`
                          console.log(data)
                         return data.products.map((product, key) => <div css= 
                         {itemCarousel}>
                         <div css={itemBody}>
                          <Grid container
                          direction="column"
                          justify="center"
                          alignItems="center">
                                <Grid item css={titulo}>{product.title}</Grid>
                                <Grid item css={[{
                                backgroundImage:  
                        `url('${require('../../images/productos/
                         5c89b2b19268c22ab85a24c5.png')}')`,
                                backgroundSize: 'contain',
                                backgroundRepeat: 'no-repeat',
                                height: 90,
                                width: 90,}]}></Grid>
                          </Grid>
                          </div>
                    </div>
                          )               
                    }}  
                    </Query>
                    </Slider>
        </div>

           )


            }
           export default SliderCarousel

0 个答案:

没有答案