【问题标题】:react-multi-carousel is not workinig反应多轮播不工作
【发布时间】:2020-05-25 09:44:24
【问题描述】:

我正在从该州获取数据。现在我想使用 react-multi-carousel 制作一个轮播滑块

我正在尝试为具有来自 API 的数据的新闻卡组件实现 https://www.npmjs.com/package/react-multi-carousel。到目前为止我的代码如下,但轮播似乎没有实现?

子组件

import Carousel from 'react-multi-carousel';
import 'react-multi-carousel/lib/styles.css'
    const responsive = {
        superLargeDesktop: {
          breakpoint: { max: 4000, min: 3000 },
          items: 5
        },
        desktop: {
          breakpoint: { max: 3000, min: 1024 },
          items: 3
        },
        tablet: {
          breakpoint: { max: 1024, min: 464 },
          items: 2
        },
        mobile: {
          breakpoint: { max: 464, min: 0 },
          items: 1
        }
    };
    const size = 15;
    const Itemlist = props.data.slice(0, size).map((item,id) => {
        return(
            <div className="item px-2 col-md-3" key={item.title}>
            <div className="alith_latest_trading_img_position_relative">
                <figure className="alith_post_thumb"> 
                <Link
                to={{
                    pathname : `/details/${id}`,
                }}
                >
                <img
                    alt=""
                    src={item.multimedia ? item.multimedia[0].url : image}
                    className="w-100 thumbnail"
                />
                </Link>
                </figure>
                <div className="alith_post_title_small">
                <Link
                to={{
                    pathname : `/details/${id}`,
                }}
                ><strong>{item.title.length > 30 ? item.title.substring(0,30) + ".." : item.title}</strong>
                </Link>
                <p className="meta">
                    <span>{`${moment(item.published_date).fromNow()}`}</span>
                </p>
                </div>
            </div>
        </div>
        )
    })
    return (
        <React.Fragment>
            <Carousel responsive={responsive}>
                  {Itemlist}
            </Carousel>
        </React.Fragment>
    );
};

父组件

    state = {
        items : []
    }
    fetchLatestNews = () => {
        api.getRealFeed()
        .then(response=>{
            this.setState({
                items : response.data.results
            });
        })
    }
    componentDidMount = () => {
        this.fetchLatestNews();
    }
    render(){
        return(
        <React.Fragment>
            <Item data={this.state.items}/>
        </React.Fragment>
)}};

【问题讨论】:

  • 介意分享您的新闻卡组件的所有代码吗?
  • 添加了@joshkmartinez。请看一下
  • 你确认props.data中有数据了吗?
  • 是的。我正在获取数据 -- {item.title}
  • 有人帮忙

标签: javascript reactjs react-multi-carousel


【解决方案1】:

我遇到了同样的问题,

看看具体的道具。您可以将类添加到容器、幻灯片或项目以添加您的 CSS 规则。就我而言,我必须为 containerClass 定义一个宽度。

<Carousel
   containerClass="carousel-container"
   itemClass="carousel-item"
>
 ... // Your carousel here

在你的css文件中:

.carousel-container {
  width: 100%;
  ...
 }

希望对你有帮助!

【讨论】:

    【解决方案2】:

    我不确定这是否会有所帮助,但是当我将道具无穷大设置为 true 时,我遇到了一个问题,即轮播变为空。 原来是因为我正在开发的网站使用 bootstrap rtl。

    为了解决这个问题,我只是将轮播容器的方向更改为 ltr。 像这样的:

    [dir="rtl"] .carousel-container{
      direction: ltr;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-10
      • 2017-04-23
      • 2012-03-10
      • 2018-07-27
      • 2014-01-05
      相关资源
      最近更新 更多