.map 返回数组中的第一个值

发布于 2025-01-09 05:36:12 字数 2670 浏览 0 评论 0原文

世界……又来了。

我被一些本来应该很简单的东西难住了,但现在我看不到它。我正在尝试映射一个简单的数组并显示值。每张卡片都应该有一个按钮,用于打开 bs 模式,该模式应该显示有关特定数组对象的更多信息。但它仅返回数组中第一个对象的信息。

我认为这里有一个我没有理解的概念,这有点令人尴尬。感谢您的帮助。

import "./styles.css";

export default function App() {
  const modalInfo = [
    { name: "james", number: "1" },
    { name: "Jackie", number: "2" },
    { name: "Ariane", number: "3" },
    { name: "Mike", number: "4" }
  ];
  return (
    <>
      <div className="App">
        {modalInfo.map((info) => (
          <div className="card">
            <h1>{info.name}</h1>
            <button
              type="button"
              class="btn btn-primary"
              data-bs-toggle="modal"
              data-bs-target="#staticBackdrop"
            >
              Show more
            </button>

            <div
              class="modal fade"
              id="staticBackdrop"
              data-bs-backdrop="static"
              data-bs-keyboard="false"
              tabindex="-1"
              aria-labelledby="staticBackdropLabel"
              aria-hidden="true"
            >
              <div class="modal-dialog">
                <div class="modal-content">
                  <div class="modal-header">
                    <h5 class="modal-title" id="staticBackdropLabel">
                      #{info.number}
                    </h5>
                    <button
                      type="button"
                      class="btn-close"
                      data-bs-dismiss="modal"
                      aria-label="Close"
                    ></button>
                  </div>
                  <div class="modal-body">{info.name}</div>
                  <div class="modal-footer">
                    <button
                      type="button"
                      class="btn btn-secondary"
                      data-bs-dismiss="modal"
                    >
                      Close
                    </button>
                    <button type="button" class="btn btn-primary">
                      Understood
                    </button>
                  </div>
                </div>
              </div>
            </div>
          </div>
        ))}
      </div>
    </>
  );
}

这是控制台中的当前错误,我从沙箱复制了代码并使用引导程序将其插入到另一个项目中:

未捕获类型错误:在不存在的对象上调用“querySelector” 实现接口元素。 findOne 选择器-engine.js:24 _showElement modal.js:217 显示 modal.js:143 和index.js:251 显示background.js:54 和index.js:251 r index.js:273 选择器引擎.js:24:43

world... again.

I'm stumped by something that should be straightforward, but right now I cant see it. I'm trying to map over a simple array and display values. Each card should have a button that opens a bs modal which should show more information on the particular array object. but instead it returns infomation only on the first object in the array.

I think there's a concept here that I'm not getting and it's a shade embarassing. Thaank's in advance for your help.

import "./styles.css";

export default function App() {
  const modalInfo = [
    { name: "james", number: "1" },
    { name: "Jackie", number: "2" },
    { name: "Ariane", number: "3" },
    { name: "Mike", number: "4" }
  ];
  return (
    <>
      <div className="App">
        {modalInfo.map((info) => (
          <div className="card">
            <h1>{info.name}</h1>
            <button
              type="button"
              class="btn btn-primary"
              data-bs-toggle="modal"
              data-bs-target="#staticBackdrop"
            >
              Show more
            </button>

            <div
              class="modal fade"
              id="staticBackdrop"
              data-bs-backdrop="static"
              data-bs-keyboard="false"
              tabindex="-1"
              aria-labelledby="staticBackdropLabel"
              aria-hidden="true"
            >
              <div class="modal-dialog">
                <div class="modal-content">
                  <div class="modal-header">
                    <h5 class="modal-title" id="staticBackdropLabel">
                      #{info.number}
                    </h5>
                    <button
                      type="button"
                      class="btn-close"
                      data-bs-dismiss="modal"
                      aria-label="Close"
                    ></button>
                  </div>
                  <div class="modal-body">{info.name}</div>
                  <div class="modal-footer">
                    <button
                      type="button"
                      class="btn btn-secondary"
                      data-bs-dismiss="modal"
                    >
                      Close
                    </button>
                    <button type="button" class="btn btn-primary">
                      Understood
                    </button>
                  </div>
                </div>
              </div>
            </div>
          </div>
        ))}
      </div>
    </>
  );
}

This is the current error in console , I copied the code from sandbox and plugged it into another project with bootstrap:

Uncaught TypeError: 'querySelector' called on an object that does not
implement interface Element. findOne selector-engine.js:24
_showElement modal.js:217 show modal.js:143 y index.js:251 show backdrop.js:54 y index.js:251 r index.js:273 selector-engine.js:24:43

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

旧情别恋 2025-01-16 05:36:12

您的控制台中一定出现了一些独特的按键错误。
为您的卡提供一个密钥,它充当您的卡的唯一id

<....
   {modalInfo.map((info, index) => (
     <div className="card" key={info.index}>  
...>

您可以提供index以及unique id,但这不是仅用于测试的正确方法。但据我所知,number 属性也增加了 1,所以这就是我将 index 保留为 key 的原因。

You must be getting some unique key error in your console.
Give a key to you card that acts a a unique id for your card.

<....
   {modalInfo.map((info, index) => (
     <div className="card" key={info.index}>  
...>

You can give index as well as a unique id but that not correct way just for testing you can. But from what I see is the number property is incrementing by 1 as well so that's why I have kept index as the key.

┼── 2025-01-16 05:36:12

id 属性在文档中必须是唯一的。您在每个 map 迭代中重复使用 staticBackdrop,因此它们会被重复。

当 Bootstrap 尝试通过 #staticBackdrop 获取模态目标时,它只会获取第一个目标,因为重复 ID 会发生这种情况。

使用沙箱链接中的 info.id 属性,尝试将其合并到 iddata-bs-target 属性中

{modalInfo.map((info) => (
  <div className="card" key={info.id}> {/* keys are important, don't use index */}
    <h1>{info.name}</h1>
    <button
      type="button"
      className="btn btn-primary"
      data-bs-toggle="modal"
      data-bs-target={`#staticBackdrop_${info.id}`}
    >
      Show more
    </button>

    <div
      className="modal fade"
      id={`staticBackdrop_${info.id}`}
      data-bs-backdrop="static"
      data-bs-keyboard="false"
      tabIndex="-1"
      aria-labelledby={`staticBackdropLabel_${info.id}`}
      aria-hidden="true"
    >
      {/* the rest of your markup goes here */}
    </div>
  </div>
))}

class 属性(应为 className)和重复的 id="staticBackdropLabel"(应获得与 staticBackdrop 类似的处理) >。

编辑 Conway v BootstrapModal(分叉)

id attributes must be unique in a document. You are re-using staticBackdrop in each map iteration so they're being duplicated.

When Bootstrap tries to grab the modal target by #staticBackdrop, it only gets the first one because that's what happens with repeated IDs.

Using the info.id properties from your sandbox link, try incorporating that into the id and data-bs-target attributes

{modalInfo.map((info) => (
  <div className="card" key={info.id}> {/* keys are important, don't use index */}
    <h1>{info.name}</h1>
    <button
      type="button"
      className="btn btn-primary"
      data-bs-toggle="modal"
      data-bs-target={`#staticBackdrop_${info.id}`}
    >
      Show more
    </button>

    <div
      className="modal fade"
      id={`staticBackdrop_${info.id}`}
      data-bs-backdrop="static"
      data-bs-keyboard="false"
      tabIndex="-1"
      aria-labelledby={`staticBackdropLabel_${info.id}`}
      aria-hidden="true"
    >
      {/* the rest of your markup goes here */}
    </div>
  </div>
))}

You also have a bunch of class attributes that should be className and a repeated id="staticBackdropLabel" that should get a similar treatment to staticBackdrop.

Edit Conway v BootstrapModal (forked)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文