reactjs - React - 循环:将数据传递给 Modal 组件
问题描述
我对 React 很陌生,我正在玩 Gatsby。
我想显示一个团队成员列表,当单击一个时,更多详细信息应以模式(react-modal)显示。收集我的数据,然后循环遍历它并为每个成员创建一个模式(基于https://codepen.io/claydiffrient/pen/pNXgqQ )。但是,我无法弄清楚如何将所有数据传递给模态。
我现在拥有的(简化):
import React from "react";
import Modal from "react-modal";
export default class Team extends React.Component {
constructor() {
super();
this.state = {
showModal: false,
};
this.handleOpenModal = this.handleOpenModal.bind(this);
this.handleCloseModal = this.handleCloseModal.bind(this);
}
handleOpenModal = (member) => {
this.setState({
showModal: true,
memberDetail: member,
});
};
handleCloseModal() {
this.setState({ showModal: false });
}
render() {
return (
<>
{this.props.data &&
this.props.data.map(({ node: member }) => (
<div key={member.frontmatter.title}>
<h3
dangerouslySetInnerHTML={{
__html: member.frontmatter.title,
}}
></h3>
<button
onClick={() =>
this.handleOpenModal(`${member.frontmatter.title}`)
}
>
Details
</button>
</div>
))}
<Modal
isOpen={this.state.showModal}
contentLabel="Modal"
onRequestClose={this.handleCloseModal}
>
{this.state.memberDetail}
<button onClick={this.handleCloseModal}>Close Modal</button>
</Modal>
</>
);
}
}
这似乎工作正常。但是,现在我想让所有成员数据对 Modal 可用。而这我想不通。数据来自 Markdown 文件,如下所示:
---
title: Some title
subTitle: some subtitle
mainImage: /images/team/hello.jpg
sequence: 20
---
Some markdown text here.
可能与如何使用反应将数据传递给模态有关。编辑还是删除?但我不确定(不能让它与那里的信息一起工作)。
解决方案
推荐阅读
- javascript - 在表中使用 Ng-repeat,替代多个 tbody?
- pyspark - PySpark 数据框下推
- elasticsearch - 如何保留某个索引比另一个更长的时间?
- python-3.x - model.fit_generator 中的“TypeError:'NoneType' 对象不可调用”
- c# - 有没有办法在 Linqpad 中创建 DbSet?
- python - 根据行中的索引添加列
- opencv - Rasberry pi 交叉编译设置中的 Config.cmake 缺失错误
- javascript - 如何从 React Burger 菜单中删除左侧阴影?
- javascript - 将箭头函数映射到映射中(映射不是函数)
- c# - WebAPI 切换数据库集合