简体   繁体   English

在一个reactjs组件中显示json数据

[英]Display json data in a reactjs component

I'm new to react and fetch data api. I dont know how to display json data in one of my component.我是新来的反应和获取数据 api。我不知道如何在我的组件之一中显示 json 数据。 I have use axios to get data from api and this is my return json data我已经使用 axios 从 api 获取数据,这是我返回的 json 数据

{
  "result": true,
  "items": [
    {
      "excerpt": "egghead is your source for the Badass Portfolio Projects you need to climb the career ladder as a modern web developer.",
      "_id": 360690368,
      "title": "Build the portfolio you need to be a badass web developer.",
      "link": "https://egghead.io/",
      "domain": "egghead.io"
    },
    {
      "excerpt": "From career choices to new purchases, use René Girard’s mimetic theory to resist the herd and forge your own path in life",
      "_id": 359605780,
      "link": "https://psyche.co/guides/how-to-know-what-you-really-want-and-be-free-from-mimetic-desire?ref=refind",
      "title": "How to know what you really want | Psyche Guides",
      "domain": "psyche.co"
    }
  ],
  "count": 2,
  "collectionId": 0
}

Here is list component:这是列表组件:

import * as React from "react"
import axios from "axios"

axios.get("https://api.abc.com", {
  method: "get",
  headers: {
    Authorization: `Bearer xxxxxxxx-xxxx-xxxx-xxxx-xxxx`,
    "Content-Type": "application/x-www-form-urlencoded",
  },
})
.then(res => {
  const bookmarks = JSON.stringify(res.data)
  console.log(bookmarks)
})
.catch(error => {
  console.error(error)
})

const IndexPage = ({ bookmarks }) => (
  <>
    <h1>Show Data</h1>
    <div>
      {bookmarks.items.map(bookmark => (
        <div>{bookmark.title}</div>
      ))}
    </div>
  </>
)

export default IndexPage

Can anyone help me in displaying the list?谁能帮我显示列表? Thanks in advance提前致谢

This is not the way how data fetching in react works... you must fetch your data in components useEffect and store your data in useState hook, which will cause rerender and you can access this data:这不是在 React 中获取数据的方式......您必须在组件useEffect中获取数据并将数据存储在useState挂钩中,这将导致重新渲染并且您可以访问此数据:

import * as React from "react"
import axios from "axios"



const IndexPage = () => (
const [bookmarks, setBookmarks] = React.useState()

React.useEffect(()=>{
axios.get("https://api.abc.com", {
  method: "get",
  headers: {
    Authorization: `Bearer xxxxxxxx-xxxx-xxxx-xxxx-xxxx`,
    "Content-Type": "application/x-www-form-urlencoded",
  },
})
.then(res => {
  const bookmarks = JSON.stringify(res.data)
  setBookmarks(bookmarks)
})
.catch(error => {
  console.error(error)
})
},[])
return(
  <>
    <h1>Show Data</h1>
    <div>
      {bookmarks.items.map(bookmark => (
        <div>{bookmark.title}</div>
      ))}
    </div>
  </>
)

edit: with custom loading solution:编辑:使用自定义加载解决方案:

import * as React from "react"
import axios from "axios"


const IndexPage = () => (
const [loading, setLoading] = React.useState(true)
const [bookmarks, setBookmarks] = React.useState()

React.useEffect(()=>{
axios.get("https://api.abc.com", {
  method: "get",
  headers: {
    Authorization: `Bearer xxxxxxxx-xxxx-xxxx-xxxx-xxxx`,
    "Content-Type": "application/x-www-form-urlencoded",
  },
})
.then(res => {
  const bookmarks = JSON.stringify(res.data)
  setBookmarks(bookmarks)
})
.catch(error => {
  console.error(error)
}).finally(()=>{setLoading(false)});
},[])

if(loading){
 return (<>Loading...</>)
}

return(
  <>
    <h1>Show Data</h1>
    <div>
      {bookmarks.items.map(bookmark => (
        <div>{bookmark.title}</div>
      ))}
    </div>
  </>
)

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM