【问题标题】:JSON React arraysJSON 反应数组
【发布时间】: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


【解决方案1】:

如果我理解得很好,你会在 JSON 中有很多对象,而你只想得到一个对象?

如果是这样,这将起作用

import ReactDOM from "react-dom";
import React, { useState } from "react";
import "./styles.css";
import data from "./data.json";

const Champions = () => {
  const [toggleShow, setToggleShow] = useState(false);
  const [currentSelectedChampion, setCurrentSelectedChampion] = useState({});

  const handleSelectChampion = (id) => {
    if (!toggleShow) setToggleShow(true);
    const currentChampion = data.filter((champ) => champ.id === id)[0];
    setCurrentSelectedChampion(currentChampion);
  };

  return (
    <div className="champions">
      {data.map((postData) => {
        return (
          <div onClick={() => handleSelectChampion(postData.id)}>
            <div className="champion">
              <img className="champion__Image" src={postData.image}></img>
              <h4 className="champion__Name">{postData.name}</h4>
              {toggleShow && currentSelectedChampion.id === postData.id && (
                <>
                  <Skills currentChampion={currentSelectedChampion} />
                  <p onClick={() => setToggleShow(false)}>Close</p>
                </>
              )}
            </div>
          </div>
        );
      })}
    </div>
  );
};

const Skills = ({ currentChampion }) => {
  return (
    <div className="spells">
      <div>
        <h1>{currentChampion.name}</h1>
        <p> {currentChampion.textQ}</p>
        <h4>{currentChampion.qInfo}</h4>
        <p> {currentChampion.textW}</p>
        <h4>{currentChampion.wInfo}</h4>
        <p> {currentChampion.textE}</p>
        <h4>{currentChampion.eInfo}</h4>
        <p> {currentChampion.textR}</p>
        <h4>{currentChampion.RInfo}</h4>
      </div>
    </div>
  );
};

ReactDOM.render(<Champions />, document.getElementById("root"));

但是,我建议您重构架构并将技能和英雄封装在一个主要组件中,并将当前英雄直接传递给他们每个人。因此,您只需过滤一次数据。 密码箱链接在这里:https://codesandbox.io/s/heuristic-blackwell-sqybi?file=/src/App.js:0-960

图片结果:

【讨论】:

  • 我希望当我点击特定英雄时,它只显示有关该英雄的信息
  • 是的。但是您放在顶部的 json 文件仅显示数组中的一个对象。您的意思是数组上通常有多个对象,并且您想获得与 champ 对应的单个对象?如果是这样,我的答案就是要走的路,您只需要按名称或 id 或您从中获得值的任何内容进行过滤
  • 它们是数组中的多个对象,当我单击打开该冠军信息的特定图像时,我想这样做
  • 我已经更新了代码,它运行良好,我还添加了一些重构,如果您有错误请告诉我或验证答案是否适合您,完整代码是顺便在codesanbox上
  • 这是在imgur.com/W76AIOM 之前,这是我点击img imgur.com/xTSdrwS 时发生的情况
猜你喜欢
  • 1970-01-01
  • 2015-10-26
  • 2018-04-21
  • 2019-04-09
  • 1970-01-01
  • 2017-09-06
  • 1970-01-01
  • 2019-06-22
  • 1970-01-01
相关资源
最近更新 更多