简体   繁体   English

如何在 reactjs 轮播中添加或显示多个项目?

[英]How to add or show multiple items in reactjs carousel?

I am trying to show multiple items in my carousel by adding a list of some filter names, but all I can see is only one at a time,我试图通过添加一些过滤器名称的列表来在我的轮播中显示多个项目,但我一次只能看到一个,

I want to achieve something like this as shown in the image, ie by default it should show 4 names and then a user clicks the next, and so on.我想实现如图所示的效果,即默认情况下它应该显示 4 个名称,然后用户单击下一个,依此类推。

What I want to show:我想展示的是:

测试图片

I do not know what did I do wrong or what I missed.我不知道我做错了什么或错过了什么。 Can someone please check the code and let me know what corrections are to be made and why is this happing in the first place?有人可以检查代码并让我知道要进行哪些更正以及为什么首先会发生这种情况吗?

Please the source code below:请在下面的源代码:

import React, { useState } from "react";

import { Button, makeStyles } from "@material-ui/core";

import KeyboardArrowLeftIcon from "@material-ui/icons/KeyboardArrowLeft";
import KeyboardArrowRightIcon from "@material-ui/icons/KeyboardArrowRight";

const useStyles = makeStyles((theme) => ({
  filter: {
    width: "95%",
    height: "25px",
    margin: "auto",
    marginTop: "5px",
    padding: "5px",
    border: "solid 1x white",
    borderRadius: "5px",
    display: "flex",
    color: "black",
    boxShadow: " 0px 2px 3px gray",
    justifyContent: "center",
    alignItems: "center"
  }
}));
const Filter = () => {
  const classes = useStyles();

  const [currentFilter, setCurrentFilter] = useState(0);

  const filterList = [
    {
      id: "1",
      title: "Action"
    },
    {
      id: "2",
      title: "Adventure"
    },
    {
      id: "3",
      title: "Comedy"
    },
    {
      id: "4",
      title: "Documentary"
    },
    {
      id: "5",
      title: "Drama"
    },
    {
      id: "6",
      title: "Family"
    },
    {
      id: "7",
      title: "Fantasy"
    },
    {
      id: "8",
      title: "History"
    },
    {
      id: "9",
      title: "Horror"
    },
    {
      id: "10",
      title: "Music"
    },
    {
      id: "11",
      title: "Mystery"
    },
    {
      id: "12",
      title: "Romance"
    },
    {
      id: "13",
      title: "Sci-Fi"
    }
  ];
  const length = filterList.length;

  const nextFilter = () => {
    setCurrentFilter(currentFilter === length - 1 ? 0 : currentFilter + 1);
  };

  const prevFilter = () => {
    setCurrentFilter(currentFilter === 0 ? length - 1 : currentFilter - 1);
  };
  console.log(currentFilter);

  if (!Array.isArray(filterList) || filterList.length <= 0) {
    return null;
  }

  return (
    <>
      <div className={classes.filter}>
        <KeyboardArrowLeftIcon color="inherit" onClick={nextFilter} />
        {filterList.map((FGneres, FGneresId) => (
          <div
            style={{ textAlign: "center", padding: "2px", width: "300px" }}
            key={FGneresId}
          >
            {FGneresId === currentFilter && <Button>{FGneres.title}</Button>}
          </div>
        ))}
        <KeyboardArrowRightIcon color="inherit" onClick={prevFilter} />
      </div>
    </>
  );
};

export default Filter;

Link for codeSandbox Source Code with Demo 带有演示的 codeSandbox 源代码链接

The result I see now is as shown in the image below:我现在看到的结果如下图所示:

测试图片

Thanks a million for help.感谢一百万的帮助。

currentFilter returns only one number, so if you compare FGneresId to currentFilter it returns only one element beacuse only one FGneresId is equel to currentFilter currentFilter 只返回一个数字,所以如果你比较 FGneresId 和 currentFilter 它只返回一个元素,因为只有一个 FGneresId 等于 currentFilter

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

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