简体   繁体   English

使用React在Carousel中渲染组件

[英]Rendering Component in Carousel using React

I want to achieve a carousel like Materialize . 我想实现像Materialize这样的轮播。 Have an API from where I am fetching the data, so according to Materialize 在我要从中获取数据的地方有一个API,所以根据Materialize

I compared the console or Materialize default and my rendered components. 我比较了控制台或Materialize默认设置和渲染组件。

I guess the problem is, it's not inheriting the properties of carousel-item 我想问题是,它不是继承carousel-item的属性


Class carousel-item is supposed to Render inside of Class carousel . carousel-item应该在类carousel内部渲染。

<div className="carousel">
// These are supposed to be dynamic, below component is not present here
  <div className="carousel-item">
  </div>
</div>

How I am trying to render the data is in this manner. 我试图呈现数据的方式就是这种方式。

    renderAlbums(){
      return this.state.albums.map(album =>
          <Card song={album.name} singer={album.artist_name} src={album.cover_photo_url}/>
      );
    }

Rendered the data <Card /> (It contains the class of carousel-item ), which is supposed to place Card containing class of carousel-item . 呈现数据<Card /> (它包含carousel-item的类),该数据应该放置Card包含carousel-item类。

class Carousel extends Component {
    state = { albums: [] };
    componentWillMount() {
      axios.get('https://cors-anywhere.herokuapp.com/https://stg-resque.hakuapp.com/albums.json')
        .then(response => this.setState({albums: response.data}));
    }
    renderAlbums(){
      return this.state.albums.map(album =>
          <div className="carousel-item"><Card key={album.name} song={album.name} singer={album.artist_name} src={album.cover_photo_url}/></div>
      );
    }
  render() {
    return (
        <div className="carousel center">
          {this.renderAlbums()}
        </div>
    );
  }
}

export default Carousel;

This is my Card component 这是我的卡组件

class Card extends Component {
  render() {
    return (

        <div className="card z-depth-4">
          <div>
            <img src={this.props.src} />
          </div>
          <p>{this.props.song}</p>

          <div className="singer">{this.props.singer}</div>
        </div>
    );
  }
}
export default Card;

EDIT: 编辑:

But it's not working the way it's expected. 但这并没有达到预期的效果。 Please suggest me, what am I doing wrong? 请建议我,我在做什么错?

First of all, there is nothing in your Carousel that says which element is active. 首先,“轮播”中没有任何内容说明哪个元素处于活动状态。 You need to have a state variable that points to the active element. 您需要有一个指向活动元素的状态变量。

Then you only need to draw [-2, -1, 0, 1, 2] offsets vs the active one. 然后,您只需要绘制[-2,-1、0、1、2]偏移量即可。 And each rendered card needs to know which offset to know their style. 并且每个渲染的卡需要知道哪个偏移量才能知道其样式。

In axios.get , I see that you are using proxy link. axios.get ,我看到您正在使用代理链接。

One reason is, it can be creating problems. 原因之一是,它可能会造成问题。


Other reason can be you are trying to put carousel-item into carousel . 另一个原因可能是您正在尝试将carousel-item放入carousel Try adding center class to both ie carousel as well as carousel-item . 尝试将center类添加到carouselcarousel-item

Check if these works. 检查这些是否有效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM