【发布时间】:2020-11-20 15:34:29
【问题描述】:
我有点卡住了,我有一个包含数组的 JSON 文件,所以我想当我点击 div 时只打开一个“数组”而不是打开它们,基本上我想在它们之间切换。如果您能帮助我,我将不胜感激。在此先感谢。编辑 我在 json 文件中有更多数组,现在它会双重渲染它们
[
{
"id":0,
"name": "Aatrox",
"image": "/champions/Aatrox_0.jpg",
"textQ":"Q",
"qInfo": "The darlking Blade Aatrox slams his greatsword down, dealing physical damage. He can swing three times, each with a different area of effect",
"image1": "/spell/AatroxQ.png",
"textW": "W",
"wInfo": "Aatrox smashes the ground, dealing damage to the first enemy hit. Champions and large monsters have to leave the impact area quickly or they will be dragged to the center and take the damage again.",
"image2" : "/spell/AatroxW.png",
"textE" : "E",
"eInfo" : " Passively, Aatrox heals when damaging enemy champions. On activation, he dashes in a direction.",
"image3" :"/spell/AatroxE.png",
"textR" : "R",
"RInfo" : "Aatrox unleashes his demonic form, fearing nearby enemy minions and gaining attack damage, increased healing, and movement speed. If he gets a takedown, this effect is extended." ,
"image4" : "/spell/AatroxR.png"
}
]
import React from 'react'
import data from './data.json'
import './Champions.css'
function Champions() {
const[toggleShow, setToggleShow]= React.useState(false)
return (
<div className='champions'>
{data.map((postData)=>{
console.log(postData);
return(
<div onClick={()=> setToggleShow((toggleShow) =>!toggleShow)}key=
{postData.id}>
<div className='champion'>
<img className='champion__Image'src={postData.image}></img>
<h4 className="champion__Name">{postData.name}</h4>
{!toggleShow &&
<Skills/>}
</div>
</div>
)
Skills Component
import React from 'react';
import "./Skills.css";
import data from './data.json'
function Skills () {
return (
<div className="spells">
{data.map((postData)=>{
console.log(postData)
return(
<div>
<img alt="123"className='image__Q'src={postData.image1}></img>
<p>{postData.textQ}</p>
<h4>{postData.qInfo}</h4>
<img alt="123"className='image__Q'src={postData.image2}></img>
<p>{postData.textW}</p>
<h4>{postData.wInfo}</h4>
<p>{postData.textE}</p>
<h4>{postData.eInfo}</h4>
<p>{postData.textR}</p>
<h4>{postData.RInfo}</h4>
</div>
)
})}
</div>
)
}
export default Skills
【问题讨论】:
-
将每个组件放入另一个组件中,然后每个组件都有自己的状态。 React 的诀窍是,不要创建巨大的单个组件,而是将它们组合成许多较小的组件。这样做,一切都会变得容易得多。
-
第一次做吗
-
@Keith 没用
-
你就是这样做的,向我们展示你的第一次尝试是你这样做的,然后我们可能会看到你是否出错了。
-
@Keith 给你
标签: javascript arrays json reactjs