简体   繁体   English

我想点击一个按钮并在 React 中显示一个特定的组件

[英]i want to click a button and display a specific component in React

im new to React, so i trying to make a pokemon web app, basically i have a list of data (Data.js) that imported it in another file (PokemonList.js), i mapped that list and rendered all the names in button form, then i want to know how i make every button display that pokemon info??我是 React 的新手,所以我尝试制作一个 pokemon web 应用程序,基本上我有一个数据列表(Data.js)将其导入另一个文件(PokemonList.js),我映射了该列表并在按钮中呈现了所有名称表格,然后我想知道我如何让每个按钮显示口袋妖怪信息??

Data.js:数据.js:

export const Data =[
    {
        id: "1",
        name: "arbok",
        imageUrl: '../pokemon_images/arbok.png',
        desc: "This is Pokemon arbok",
        Height : "200 cm",
        Weight: "100 kg",
        Stat : {
                hp : "80",
                attack : "82",
                defense : "83",
                special_attack : "100",
                special_defense : "100",
                speed : "80",
               },
    },

    {
        id: "2",
        name: "arcanine",
        imageUrl: "",
        desc: "This is Pokemon arcanine",
        Height : "210 cm",
        Weight: "110 kg",
        Stat : {
                hp : "81",
                attack : "83",
                defense : "84",
                special_attack : "110",
                special_defense : "110",
                speed : "81",
               },
    },

PokeList.js PokeList.js

import { Data } from "./Data";
import "./PokeList.css"
import { useState } from "react";




function PokeList() {
    const [pokeInfo , setPokeInfo] = useState({
        name: "",
        desc: ""
      })
   
    const handleClick=() => {
        setPokeInfo({  
        })
    }


    return (
        <div className="app-container">
          <div className="pokemon-container">
            <div className="all-container">
                
               {Data.map((el)=> {
                return (
                    <>
                        <button onClick={handleClick(el)}> {el.name} </button> 
                     
                    </>
                )
               }               
                )}
                

            </div>
           </div>
        </div>
        
    )
}

export default PokeList;

As you guys can see the code is incomplete and i really have no idea what to do你们可以看到代码不完整,我真的不知道该怎么做

You'll simply need to do these changes您只需要进行这些更改

Change handleClick function to this把handleClick function改成这个

const handleClick=(el) => {
    setPokeInfo({
      name: el.name,
      desc: el.desc
    })
}

and return function to this并将 function 返回给这个

return (
        <>
          <button onClick={handleClick(el)}> {el.name} {el.desc}</button> 
        </>
)

This would not 100% map as the answer to your question.这不会 100% map 作为您问题的答案。 But if you share a working JSFiddle with your code - we'll be able to help you out more但是,如果您与您的代码共享一个有效的 JSFiddle - 我们将能够为您提供更多帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 每次单击按钮时,我都想重定向到一个组件,我尝试了 Link、React 中的 Routes,但它不起作用 - Every time I click on a Button I want to redirect to a component I tried Link , Routes in React but its not working 当我单击 class 组件 React JS 中的按钮时,我希望图像发生变化 - I want the image to change when i click the button in class component React JS 我有三个下拉按钮我想单击特定的下拉列表并弹出列表并显示在该特定的单击下拉列表上,而不是全部 - I have three drop down button I want to click the specific dropdown and pop up the list and display on the that specific click dropdown , not in all 我需要在按钮上单击两次以显示组件 - I need to click twice on ,the button to display the component 我想单击父组件以在反应中呈现子组件 - I want to click a parent component to render a child component in react 单击按钮显示不同的组件(反应组件类) - Display a different components in a click of a button (react component class) 单击“特定”按钮时如何显示特定值 - How to Display Specific value when I click Specific button 单击按钮显示新组件 - Display a new component with button click 单击按钮时如何获取我想要的 select 数据(React JS) - How to select data that i want when click button (React JS) 组件未在按钮单击时更新,反应 - Component is not updating on Button Click, React
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM