.map 返回数组中的第一个值
世界……又来了。
我被一些本来应该很简单的东西难住了,但现在我看不到它。我正在尝试映射一个简单的数组并显示值。每张卡片都应该有一个按钮,用于打开 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的控制台中一定出现了一些独特的按键错误。
为您的卡提供一个
密钥
,它充当您的卡的唯一id
。您可以提供
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 uniqueid
for your card.You can give
index
as well as aunique id
but that not correct way just for testing you can. But from what I see is thenumber
property is incrementing by 1 as well so that's why I have keptindex
as thekey
.id
属性在文档中必须是唯一的。您在每个 map 迭代中重复使用staticBackdrop
,因此它们会被重复。当 Bootstrap 尝试通过
#staticBackdrop
获取模态目标时,它只会获取第一个目标,因为重复 ID 会发生这种情况。使用沙箱链接中的
info.id
属性,尝试将其合并到id
和data-bs-target
属性中。
class
属性(应为className
)和重复的id="staticBackdropLabel"
(应获得与staticBackdrop
类似的处理) >。id
attributes must be unique in a document. You are re-usingstaticBackdrop
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 theid
anddata-bs-target
attributesYou also have a bunch of
class
attributes that should beclassName
and a repeatedid="staticBackdropLabel"
that should get a similar treatment tostaticBackdrop
.