简体   繁体   English

如何保存和显示从数据库检索到的数据

[英]How to save and display data retrieved from database

So I've retrieved data in JSON format using GET request then I'm facing a problem in saving into my state so that I can display them into my span / div / h1 using map function . So I've retrieved data in JSON format using GET request then I'm facing a problem in saving into my state so that I can display them into my span / div / h1 using map function . Here's how my JSON data looks like:这是我的JSON数据的样子:

[
   {
        "projectName": "SHIBA INU",
        "projectToken": "SHIB",
        "tokenPrice": 0.2,
        "saleStart":{
            "$date": "2021-12-20T20:00:00.00Z"
        },
   {
        "projectName": "BITCOIN",
        "projectToken": "BTC",
        "tokenPrice": 200,
        "saleStart":{
            "$date": "2021-12-20T20:00:00.00Z"
   }
]

So I'm trying to do this way but I dont think it will work:所以我正在尝试这样做,但我认为它不会起作用:

const [projectData, setProjectData] = useState({})
useEffect(() => {
      axios.get(`my api`).then(res => {
        for(let i=0; i<res.data.length; i+1){
          setProjectData(...projectData, res.data[i])
        }
      }).catch(e => console.log("error: ", e));
    },[]);

I'm not sure whether I should save them into an array or an object我不确定是否应该将它们保存到数组或 object

From the data in your example, it seems like you're trying to store an array of objects, so you'll want to initialize your state with an array, and then add the items from the response to the array as well.从示例中的数据来看,您似乎正在尝试存储对象数组,因此您需要使用数组初始化 state,然后将响应中的项目也添加到数组中。 You also don't need the loop: you can spread all of the array items into a new array along with the existing state in one step:您也不需要循环:您可以一步将所有数组项与现有 state 一起传播到一个新数组中:

const [projectData, setProjectData] = useState([]);

useEffect(() => {
  axios.get(`my api`).then(res => {
    setProjectData([...projectData, ...res.data]);
  }).catch(e => console.log("error: ", e));
}, []);

First of all you are not updating your i value in your for loop.首先,您没有在 for 循环中更新i值。 Since your i value never will be less than res.data.length, it will loop infinitely.由于您的i值永远不会小于 res.data.length,因此它将无限循环。 Instead you should use i++ to increment it at each iteration:相反,您应该使用i++在每次迭代时递增它:

for(let i=0; i<res.data.length; i++)

Other than that setting your state like that is problematic.除了像这样设置你的 state 是有问题的。 setState in React is async which means when you use it and access your projectData it won't promise you that you are getting the updated state right away. React 中的setState是异步的,这意味着当您使用它并访问您的projectData时,它不会 promise 您立即获得更新的 state。 Instead you should try to update your state at once in your case.相反,您应该尝试在您的情况下立即更新您的 state。 Also check Why is setState in reactjs Async instead of Sync?还要检查为什么 reactjs 中的 setState 是异步而不是同步?

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

相关问题 使用从数据库检索的数据显示多个实时倒计时 - Display multiple live countdown using data retrieved from database 在从mysql数据库检索的模式中的字段中显示数据 - To display data in the fields in the modal retrieved from the mysql database 显示使用JavaScript从MySQL数据库检索的Blob数据 - Display Blob data retrieved from MySQL Database using JavaScript 如何将数据保存在从json检索的不同变量中 - How to save data in different variables retrieved from json 如何在iOS的React Naive中显示从Firebase检索的数据 - How to display data retrieved from firebase in react naive for ios 如何通过 html 显示从 JavaScript 检索到的数据 - How to display retrieved data from JavaScript through html 将从数据库检索到的记录显示在对话框中 - To display the records retrieved from database into a dialog box 如何将可点击的行连接到从数据库检索的特定数据部分? - How to connect clickable row to specific data sections retrieved from database? 如何打印从Node.js中的mongoDB数据库检索的数据? - How to print data, retrieved from mongoDB database in Node.js? 如何将从数据库中检索的数据设置为div内容? - how to set the data retrieved from the database as the div content?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM