使用地图时在对象内获取数据

发布于 2025-02-07 00:46:46 字数 801 浏览 1 评论 0原文

我对自己的无知事先提前很抱歉,但是自编码以来已经有一段时间了,我需要对这个基本的东西进行一些帮助,我试图访问名称,并在我单击按钮时在屏幕上显示它,但是我只是无法弄清楚,您能帮我并解释您所做的事情,以便我下次知道吗?看着有关如何做的手册和文章,但你们只是更好。 我的代码:

功能(pokeapi.ts):

import React from "react";
import axios from 'axios'


export const getPokemon=()=>{
    axios.get("https://pokeapi.co/api/v2/pokemon?limit=151").then((response)=>{
response.data.results.map((cont : string,index : number) => console.log(
        cont
      ));
    })

  }

app.tsx:

   import './App.css';
    import React from 'react';
    import {getPokemon} from './func/pokeAPI';
    
    function App() {
    
      return (
    <div>
    <button onClick={getPokemon}>Cli</button>
    </div>
    
      );
    }
    
    export default App;

I'm sorry in advance for my ignorance, But its been a while since I coded, and I need a little help with this basic thing, Im trying to reach name, and display it on screen when I click a button, but i just can't figure it out, can u help me and explain what you did so I can know for the next time? looked manuals and articles about how to do it but you guys are just simply better.
my code:

functional (pokeAPI.ts):

import React from "react";
import axios from 'axios'


export const getPokemon=()=>{
    axios.get("https://pokeapi.co/api/v2/pokemon?limit=151").then((response)=>{
response.data.results.map((cont : string,index : number) => console.log(
        cont
      ));
    })

  }

app.tsx:

   import './App.css';
    import React from 'react';
    import {getPokemon} from './func/pokeAPI';
    
    function App() {
    
      return (
    <div>
    <button onClick={getPokemon}>Cli</button>
    </div>
    
      );
    }
    
    export default App;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

孤芳又自赏 2025-02-14 00:46:46

干得好!由于您正在使用TypeScript,因此我还创建了一个Pokemondata接口。

import React from 'react'
import axios from 'axios'
import "./styles.css";

interface PokemonData {
  name: string
  url: string
}


export default function App() {
  const [pokemons, setPokemons] = React.useState<null|PokemonData[]>(null)

  const onClick = async () => {
    axios.get("https://pokeapi.co/api/v2/pokemon?limit=151").then((response)=>{
      console.log(response.data.results)
      setPokemons(response.data.results)
     })
  }

  return (
    <div className="App">
      <button onClick={onClick}>Get pokemon</button>
     {pokemons?.map((pokemon) => (
       <div key={pokemon.name}>
         <p>{pokemon.name}</p>
         <p>{pokemon.url}</p>
       </div>
     ))}
    </div>
  );
}

我还写了 codesandbox 在这里,您可以查看它的

编辑:返回语句的另一个版本,如果您的项目版本不支持可选的链接

 return (
    <div className="App">
      <button onClick={onClick}>Get pokemon</button>
     {pokemons && pokemons.map((pokemon) => (
       <div key={pokemon.name}>
         <p>{pokemon.name}</p>
         <p>{pokemon.url}</p>
       </div>
     ))}
    </div>
  );
}

Here you go! Since you're using typescript, I created a PokemonData interface as well.

import React from 'react'
import axios from 'axios'
import "./styles.css";

interface PokemonData {
  name: string
  url: string
}


export default function App() {
  const [pokemons, setPokemons] = React.useState<null|PokemonData[]>(null)

  const onClick = async () => {
    axios.get("https://pokeapi.co/api/v2/pokemon?limit=151").then((response)=>{
      console.log(response.data.results)
      setPokemons(response.data.results)
     })
  }

  return (
    <div className="App">
      <button onClick={onClick}>Get pokemon</button>
     {pokemons?.map((pokemon) => (
       <div key={pokemon.name}>
         <p>{pokemon.name}</p>
         <p>{pokemon.url}</p>
       </div>
     ))}
    </div>
  );
}

I also wrote a Codesandbox here so you can check it out

Edit: Another version of the return statement if optional chaining isn't supported on your project's version

 return (
    <div className="App">
      <button onClick={onClick}>Get pokemon</button>
     {pokemons && pokemons.map((pokemon) => (
       <div key={pokemon.name}>
         <p>{pokemon.name}</p>
         <p>{pokemon.url}</p>
       </div>
     ))}
    </div>
  );
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文