- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我是 React 和处理 JSON 的新手,但我在这里阅读了很多。所以我一直在尝试将从 pokeapi 获取的 JSON 传递给变量这是我到目前为止所得到的:
getPokemons=(id)=> {
return fetch(`https://pokeapi.co/api/v2/pokemon/${id}/`)
.then(res => {
if (res.ok) {
//return res.json()
console.log(res.json())
}
})
}
我试过这个方法:Return json object to render method - React.js 但没用
最佳答案
您需要使用 setState
使 API 结果成为状态的一部分。通过将此组件复制到您的代码中来试用此组件:
class PokemonFinder extends React.Component {
constructor(props) {
super(props);
this.state = {};
}
getPokemon(id) {
fetch(`https://pokeapi.co/api/v2/pokemon/${id}/`)
.then(results => results.json())
.then(data => {
this.setState({data});
});
}
render() {
return <div>
<input value={this.state.id}
onChange={(ev) => this.setState({
value: ev.target.value
})}
/>
<button onClick={() => this.getPokemon(this.state.id)}>Get Pokemon</button>
<div>{JSON.stringify(this.state.data, null, 2)}</div>
</div>;
}
}
ReactDOM.render(<PokemonFinder/>, document.getElementById('app'));
<script src="https://unpkg.com/react@16.7.0-alpha.0/umd/react.development.js"></script>
<script src="https://unpkg.com/react-dom@16.7.0-alpha.0/umd/react-dom.development.js"></script>
<div id="app"></div>
关于javascript - Reactjs 和 Pokeapi JSON,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53220804/
最近开始学习react。我正在使用 pokeAPI 制作一个图鉴应用程序。在卡片上,我需要获取每个口袋妖怪的类型。下面是我如何获取和保存口袋妖怪数据的示例。 useEffect(() => {
我有一个应用程序可以搜索宠物小 Sprite 的名称并读取数据,例如。姓名、高度、体重。现在说到能力,我无法理解能力名称的值(value)。 这是我的 app.js import React, { C
我是 React 和处理 JSON 的新手,但我在这里阅读了很多。所以我一直在尝试将从 pokeapi 获取的 JSON 传递给变量这是我到目前为止所得到的: getPokemons=(id)=> {
我是 Angular 的新手,我正在通过尝试使用 pokeapi 为每个 pokemon 拉动进化链来学习一些东西,但由于嵌套很深,所以遇到了困难。 一个典型的响应对象是这样返回的: { "bab
我正在尝试从 PokeAPI 获取进化链,stackoverflow 中的另一个答案帮助了我 How to get pokemon's evolution chain 顺便说一句,我不明白如何从“ev
我正在尝试在我正在开发的应用程序中使用 PokéAPI 查找并使用语言 JSON 端点的英文翻译。在提交对如下所示网址之一的调用时,我需要使用翻译。不幸的是,英语语言键在数组响应中并不总是按相同的顺序
我是一名优秀的程序员,十分优秀!