英文:
How do I fetch some data from external API in react JS?
问题
componentDidMount() {
const url = "https://jsonplaceholder.typicode.com/posts";
fetch(url)
.then((response) => {
return response.json();
})
.then((json) => {
let posts;
if(json.userId > 16 ){
return console.log();
}
});
英文:
componentDidMount() {
const url = "https://jsonplaceholder.typicode.com/posts";
fetch(url)
.then((response) => {
return response.json();
})
.then((json) => {
let posts;
if(json.userId > 16 ){
return console.log();
}
});
I'm trying to fetch 15 data from external API which has like 100 data, here's the code I'm currently writing but when I run it, it won't show any data
答案1
得分: 1
以下是翻译好的部分:
"看起来你在使用 fetch
函数从外部 API 获取数据方面是正确的轨道上。但是,我注意到你的代码存在一些问题,可能导致它不显示任何数据。
首先,在你的 fetch
调用中,你只在 then
块中检查来自 API 的响应。然而,你还需要处理可能发生的任何错误。你可以通过在 then
块的末尾添加一个 .catch
块来实现这一点。
其次,在你的 json
响应中,你只检查了 userId
是否大于 16,但你实际上没有对数据做任何处理。要显示数据,你需要将其设置为组件状态或直接在组件的 render
方法中渲染它。
这是一个示例代码片段,从 JSONPlaceholder API 中获取前 15 个帖子并将它们存储在组件状态中:
class MyComponent extends React.Component {
constructor(props) {
super(props);
this.state = {
posts: [],
error: null,
};
}
componentDidMount() {
const url = "https://jsonplaceholder.typicode.com/posts?_start=0&_limit=15";
fetch(url)
.then((response) => {
if (!response.ok) {
throw new Error("Network response was not ok");
}
return response.json();
})
.then((data) => {
this.setState({ posts: data });
})
.catch((error) => {
this.setState({ error: error });
});
}
render() {
const { posts, error } = this.state;
if (error) {
return <div>Error: {error.message}</div>;
}
return (
<div>
<h1>Posts</h1>
<ul>
{posts.map((post) => (
<li key={post.id}>{post.title}</li>
))}
</ul>
</div>
);
}
}
在这段代码中,我们使用 fetch
函数通过向 URL 添加 _start=0
和 _limit=15
查询参数来从 API 获取前 15 个帖子。我们还使用 .catch
块处理可能发生的任何错误。
当成功检索到数据时,我们使用 this.setState
将其存储在组件状态中。在 render
方法中,我们在状态中映射 posts
数组,以在无序列表中呈现每个帖子的标题。
如果发生错误,我们将显示错误消息而不是帖子列表。
希望这有所帮助!如果你有任何进一步的问题,请告诉我。"
英文:
It looks like you're on the right track with using the fetch
function to retrieve data from an external API. However, I noticed a few issues with your code that may be causing it to not display any data.
First, in your fetch
call, you're only checking the response from the API in the then
block. However, you need to also handle any errors that may occur. You can do this by adding a .catch
block at the end of the then
block.
Second, in your json
response, you're only checking if the userId
is greater than 16, but you're not actually doing anything with the data. To display the data, you need to set it to the component state or render it directly in your component's render
method.
Here's an example code snippet that fetches the first 15 posts from the JSONPlaceholder API and stores them in the component state:
class MyComponent extends React.Component {
constructor(props) {
super(props);
this.state = {
posts: [],
error: null,
};
}
componentDidMount() {
const url = "https://jsonplaceholder.typicode.com/posts?_start=0&_limit=15";
fetch(url)
.then((response) => {
if (!response.ok) {
throw new Error("Network response was not ok");
}
return response.json();
})
.then((data) => {
this.setState({ posts: data });
})
.catch((error) => {
this.setState({ error: error });
});
}
render() {
const { posts, error } = this.state;
if (error) {
return <div>Error: {error.message}</div>;
}
return (
<div>
<h1>Posts</h1>
<ul>
{posts.map((post) => (
<li key={post.id}>{post.title}</li>
))}
</ul>
</div>
);
}
}
In this code, we're using the fetch
function to retrieve the first 15 posts from the API by adding _start=0
and _limit=15
query parameters to the URL. We're also handling any errors that may occur using the .catch
block.
When the data is successfully retrieved, we're storing it in the component state using this.setState
. In the render
method, we're mapping over the posts
array in the state to render each post's title in an unordered list.
If an error occurs, we're displaying an error message instead of the post list.
I hope this helps! Let me know if you have any further questions.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论