循环浏览从一个州映射的列表项(javascript)

用户67

您好,圣诞快乐,

我有一个包含嵌套数据的项目列表,这些数据以组件状态保存。我只是试图将项目的信息映射到所有屏幕,同时允许用户使用下一个和上一个按钮在它们之间循环。

我的问题:在项目列表中增加或减少时,应用程序会出错。

问题:如何将当前对象更改为下一个或上一个项目?

这是一个工作示例:https : //codesandbox.io/s/peaceful-albattani-g3r6u?file=/src/App.js

或者这是整个app.js:

import React, { useState } from "react";
import "./styles.css";
import {
  ButtonDropdown,
  DropdownToggle,
  DropdownMenu,
  DropdownItem,
  Button
} from "reactstrap";
import "bootstrap/dist/css/bootstrap.min.css";

export default function App() {
  const [dropdownOpen, setOpen] = useState(false);
  const toggle = () => setOpen(!dropdownOpen);
  const [currentIndex, setCurrentIndex] = useState({
    itemIndex: 0,
    locationIndex: 0
  });
  const [items, setItems] = useState([
    {
      item: 123,
      locations: [
        { location: "loc1", count: 100 },
        { location: "loc2", count: 200 }
      ]
    },
    {
      item: 456,
      locations: [
        { location: "loc3", count: 200 },
        { location: "loc4", count: 300 }
      ]
    },
    {
      item: 789,
      locations: [
        { location: "loc5", count: 100 },
        { location: "loc6", count: 600 }
      ]
    }
  ]);

  const deleteItem = (item) => {};

  const ddItemStyle = {
    display: "flex",
    flexDirection: "row",
    alignItems: "center",
    justifyContent: "center"
  };

  return (
    <div className="App">
      <ButtonDropdown isOpen={dropdownOpen} toggle={toggle}>
        <DropdownToggle caret>locations</DropdownToggle>
        <DropdownMenu>
          {items[currentIndex.itemIndex].locations.map((location) => {
            return (
              <DropdownItem style={ddItemStyle}>
                <Button
                  onClick={() => {
                    deleteItem(location.location);
                  }}
                >
                  Delete
                </Button>
                : {location.location}{" "}
              </DropdownItem>
            );
          })}
        </DropdownMenu>
      </ButtonDropdown>
      <div style={{ height: "50%", width: "50%" }}>
        Item: {items[currentIndex.itemIndex].item}
      </div>
      <Button
        onClick={() => {
          setCurrentIndex({
            itemIndex: currentIndex.itemIndex + 1,
            locationIndex: 0
          });
        }}
      >
        Prev
      </Button>
      <Button
        onClick={() => {
          setCurrentIndex({
            itemIndex: currentIndex.itemIndex - 1,
            locationIndex: 0
          });
        }}
      >
        Next
      </Button>
    </div>
  );
}

多梅尼克

首先,你的上一张下一个按钮,需要被交换左右。

另外,您还需要防止越界。这就是为什么您会收到该错误的原因:

<Button
    onClick={() => {
        // out of bounds check
        if(currentIndex.itemIndex <= 0) return;
        setCurrentIndex({
        itemIndex: currentIndex.itemIndex - 1,// decrement
        locationIndex: 0
        });
    }}
>
    Prev
</Button>
<Button
    onClick={() => {
        // out of bounds check
        if(currentIndex.itemIndex >= items.length - 1) return;
        setCurrentIndex({
        itemIndex: currentIndex.itemIndex + 1,// increment
        locationIndex: 0
        });
    }}
>
    Next
</Button>

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

打印在for循环中引发错误的列表项,并继续下一个列表项

如何使用按钮浏览下一个和上一个列表项?

在 for 循环中的另一个列表项之后创建一个列表名称

单击列表项时如何从一个片段切换到另一个片段?

将列表项从一个 ul 元素移动到另一个 ul 元素

尝试定位一个映射列表项并在反应中更改其图标

如何遍历一个列表,其中列表项不断添加到循环中?

保持上一个项目在列表项中循环访问的一种优雅方法

在jQuery UI排序中将列表项从一个列表移动到另一个列表后,如何更新排序顺序号

使用循环将数据从一个表映射到另一个表

XForms:为带有循环的所有列表项插入一个子项

内有另一个循环时,列表项的迭代速度太慢

使用for循环在每个列表项旁边添加一个复选框

从一个视图映射具有嵌入式列表的实体

Firebase & javascript - 从一个列表中排除项目到另一个

单击另一个列表项中的一个列表项

如何获取特定列表项并将两个值从一个活动传递到另一个活动?

选择一个不带CSS子列表的列表项

如何为每个列表项生成一个列表

删除另一个列表中的列表项

访问上一个列表的列表项

使用JavaScript中的循环从一个对象创建两个对象时出错

Javascript 活动类仅应用于第一个列表项

循环浏览一个列表,然后将项目追加到三个列表中,一个接一个

从两个列表中提取一个共同元素,并通过避免重复创建一个字典,从一个列表映射到另一个列表

用于将列表从一个Dataframe行映射到另一Dataframe行的矢量化方法

numpy:有没有一种方法可以从一个没有外部循环的映射序列中创建一个数组?

使用p循环浏览列表,然后单击一个按钮

Clojure:循环浏览一个集合,同时循环浏览另一个集合?