亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

使用reactjs渲染JSON數據(來自reddit API)

使用reactjs渲染JSON數據(來自reddit API)

寶慕林4294392 2023-07-06 18:15:18
React 非常新,所以我可能會以錯誤的方式處理這個問題...我希望我的應用程序從文本輸入字段獲取輸入,從 reddit API 檢索 JSON(url 是根據文本輸入構建的),然后渲染來自 JSON 的數據,循環遍歷每個條目。我正在使用 useState 來觸發數據渲染。我可以成功檢索數據并輸出特定值,但我希望能夠有一個循環將數據動態輸出到各種 HTML 元素中。到目前為止,這是我所擁有的,可以讓我輸出一些特定值作為示例:import React, { useState } from 'react';const App = () => {  const [retrievedData, setRetrievedData] = useState([])    const runSearch = async() => {    const searchInput = document.getElementById('searchInput').value    const searchUrl = 'https://www.reddit.com/r/' + searchInput + '/new/.json?limit=5'    const response = await fetch(searchUrl)    const redditResponse = await response.json()    setRetrievedData(<>    <>{JSON.stringify(redditResponse.data.children[0].data.author)}</p>    <p>{JSON.stringify(redditResponse.data.children[0].data.title)}</p>    </>)  }  return (    <>      <section>        <input type="text" id='searchInput' placeholder='Enter a subreddit...'></input>        <button onClick={runSearch}>          Get Data        </button>        <div>{retrievedData}</div>      </section>    </>  );};export default App;下面是從 reddit API 檢索的 JSON 示例,僅使用我在上面的代碼中使用的示例值進行了精簡:{  "kind": "Listing",  "data": {    "modhash": "",    "dist": 5,    "children": [      {        "kind": "t3",        "data": {          "author": "author1",          "title": "title1"        }      },      {        "kind": "t3",        "data": {          "author": "author2",          "title": "title2"        }      },      {        "kind": "t3",        "data": {          "author": "author3",          "title": "title3"        }      },      {        "kind": "t3",        "data": {          "author": "author4",          "title": "title4"        }      },      {        "kind": "t3",        "data": {          "author": "author5",          "title": "title5"        }      }    ],    "after": "t3_jnu0ik",    "before": null  }}我見過各種不同的渲染 JSON 數據的方法,其中許多顯示循環和/或 .map() 方法,但我似乎無法讓這些方法工作,并想知道這是否是一個問題使用狀態。也許我應該以其他方式呈現數據?
查看完整描述

1 回答

?
呼喚遠方

TA貢獻1856條經驗 獲得超11個贊

您不需要設置jsx狀態,您可以直接迭代子數據map


嘗試這個


const App = () => {

  const [retrievedData, setRetrievedData] = useState([])

  

  const runSearch = async() => {

    const searchInput = document.getElementById('searchInput').value

    const searchUrl = 'https://www.reddit.com/r/' + searchInput + '/new/.json?limit=5'

    const response = await fetch(searchUrl)

    const redditResponse = await response.json()

    if (redditResponse.data.children && redditResponse.data.children.length) {

      setRetrievedData(redditResponse.data.children)

    }

  }


  return (

    <>

      <section>

        <input type="text" id='searchInput' placeholder='Enter a subreddit...'></input>

        <button onClick={runSearch}>

          Get Data

        </button>

        <div>

          {

            retrievedData.map((children, index) => {

              return (

                <div key={children.data.author + index}>

                  <div>Kind: { children.kind }</div>

                  <div>Author: { children.data.author }</div>

                  <div>Title: { children.data.title }</div>

                </div>

              )

            })

          }

        </div>

      </section>

    </>

  );

};


查看完整回答
反對 回復 2023-07-06
  • 1 回答
  • 0 關注
  • 311 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號