使用地图时在对象内获取数据
我对自己的无知事先提前很抱歉,但是自编码以来已经有一段时间了,我需要对这个基本的东西进行一些帮助,我试图访问名称,并在我单击按钮时在屏幕上显示它,但是我只是无法弄清楚,您能帮我并解释您所做的事情,以便我下次知道吗?看着有关如何做的手册和文章,但你们只是更好。 我的代码:
功能(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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
干得好!由于您正在使用TypeScript,因此我还创建了一个
Pokemondata
接口。我还写了 codesandbox 在这里,您可以查看它的
编辑:
返回
语句的另一个版本,如果您的项目版本不支持可选的链接Here you go! Since you're using typescript, I created a
PokemonData
interface as well.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