javascript - React.JS:以编程方式创建表头的问题
问题描述
我对 React 很陌生,并且在以编程方式呈现表的标题时遇到问题:基本上我希望每个标题都来自一个数组。我在构造函数中创建数组,稍后引用它。现在我只想获取工作人员的标题,但稍后我想为每个单元格设置一个数组中的值。我到目前为止是这样的:
import React from "react";
import firebase from "../firestore";
const db = firebase.firestore();
const docRef = db.collection("tournaments").doc("Siege");
class Table extends React.Component {
constructor(props) {
super(props);
this.state = {
matches: [],
headers:[],
};
}
componentDidMount() {
let headers =["Time"];
let matches = docRef.collection("Matches").get().then(querySnapshot => {
matches = querySnapshot.docs.map(doc => doc.data());
this.setState( {matches});
docRef.get().then(snapshot=> {
var noPitches=snapshot.data().numberPitches;
for (let i=1; i<noPitches; i++){
headers.push("Pitch " + i);
}
});
this.setState({headers});
});
}
render() {
return (
<div>
<table id='mytable'>
<thead>
<tr>
{ this.state.headers.map((header,index) =>
<th key={index}>{header} </th>
)}
</tr>
</thead>
</table>
</div>
)
}
}
export default Table;
当表格被渲染时,我只能看到第一个标题,时间。将标题数组输出到控制台时,我可以看到,它实际上执行了 3 次日志。前两次,headers 和 this.state.headers 都是空的,第三次它记录了正确的值。我对正在发生的事情感到非常困惑,任何帮助将不胜感激。
解决方案
推荐阅读
- json - 顶部节点为数字时的 JSON 解析
- c++ - operator->() 是否保证可用于标准 C++ 容器的迭代器?
- amazon-s3 - Terraform如何限制s3对象公开
- batch-file - 如何在我的游戏中制作随机关卡选择系统?
- javascript - Discord.js 加载字符串
- node.js - 如何在 mongo 中使用 limit、match、sort 和 groupby 以及包含字符串的 toInt?
- sql - SQL:根据关系表将两个表组合成相同的列
- python - 使用 Jinja 循环访问数据库表时出现错误:“数据库名称”未定义
- amazon-web-services - AWS 如何使用来自 Lamda 函数 Nodejs 的私有 DNS 端点调用 SSM 参数存储
- python - 是否可以结合pythontex将TikZ图片外化