this.periodicTable[]
正如您在下面的代码中看到的那样,我正在努力根据给出的信息在 HTML 中生成周期表并稍后渲染它。我怎样才能让它工作?首先我是手工制作的(全部在用 HTML 编写的 render() 部分),但后来我发现生成它会更好。一切正常,直到我切换到生成 html 而不是编写它。我也想在网站的其他部分使用周期表数据,所以这个是必要的
导入 React 的东西
class PeriodicTable extends React.Component{
constructor(props){
super(props);
this.state = {
show: true,
};
this.periodicTable = [
// 1 row
this.hydrogen = {
blank: false,
name: "hydrogen",
short: "H",
z: 1,
mass: 1.006,
},
this.blank = {
blank: true
},
this.blank = {
blank: true
},
this.blank = {
blank:true
},
this.blank = {
blank: true
},
this.blank = {
blank: true
},
this.blank = {
blank: true
},
this.blank = {
blank: true
},
this.blank = {
blank: true
},
this.blank = {
blank: true
},
this.blank = {
blank: true
},
this.blank = {
blank: true
},
this.blank = {
blank: true
},
this.blank = {
blank: true
},
this.blank = {
blank: true
},
this.blank = {
blank: true
},
this.blank = {
blank: true
},
this.hellium = {
blank: false,
name: "hellium",
short: "He",
z: 2,
mass: 4.0026,
},
// 2 row
]
this.output = "";
}
generatePeriodicTable = () =>{
for(let i = 0; i < this.periodicTable.length; i++){
if(this.periodicTable[i].blank){
this.output += <div class = "periodicCard" class="blank"></div>;
}else{
this.output +=
<div class="periodicCard">
<sup class="periodicZ">{this.periodicTable[i].z}</sup>
<div class='center'>
<strong class="periodicElement">{this.periodicTable[i].short}</strong>
</div>
<div class='center'>
<p class="periodicMass">{this.periodicTable[i].mass}</p>
</div>
</div>;
}
}
return this.output;
}
showLearnItPage = () =>{
this.setState({show: false});
}
render(){
if(this.state.show){
return(
<div>
<h2>Periodic<sup class="sup">table</sup></h2>
<main class="biggrid">
{this.generatePeriodicTable}{this.output}
</main>
<div class='center'>
<button class='periodic-table-page-button' onClick={this.showLearnItPage}>Back</button>
</div>
</div>
);
}else{
return( <LearnItPage /> );
}
}
}
export default PeriodicTable;```