简体   繁体   English

如何将公用文件夹.json 文件提取到反应组件中

[英]How to fetch public folder .json file into react component

I am trying to calculate the array and show the number on the dashboard.我正在尝试计算数组并在仪表板上显示数字。 I tried to fetch('data/users.json) but got an error that shows 0. Need to calculate the id from two JSON files located at public folder inside data folder, you can see on the code below.我尝试fetch('data/users.json)但出现显示 0 的错误。需要从位于数据文件夹内的公共文件夹中的两个 JSON 文件计算 ID,您可以在下面的代码中看到。

import React from 'react'
import '../Navbar/navbar.css'
import '../Dashboard/dashboard.css'
// import UserList from "./data/users.json"

const Dashboard = () => {

    
    return (
        <div>
            <main>
                <h1 className= "heading">Welcome to Dashboard</h1>
                    {/* <p> User data list : {Object.keys(UserData).length}</p>
                    <p>Suscriber list: {Object.keys(Suscriber).length}</p> */}
                <p>
                    Lorem
                    ipsum dolor sit, amet consectetur adipisicing elit. Placeat accusantium
                    rerum dolorem odio amet id blanditiis nobis incidunt consequuntur
                    molestiae doloribus corrupti quidem dolor ex, assumenda beatae dolore?
                    Voluptatem, ab?Lorem ipsum dolor sit amet consectetur, adipisicing elit.
                </p>

                <div className="users_view">
                    <div className="flexs">
                        <div className="total_users">
                            <h1>Total Users: {Object.keys(fetch('data/users.json')).length} </h1>
                        </div>
                    </div>
                    <div className="flexs">
                        <div className="total_users">
                            <h1>Subscribed user: {Object.keys(fetch('data/subscriptions.json')).length}</h1>
                        </div>
                    </div>
                </div>
                
                
            
            </main>
        </div>
    )
}

export default Dashboard

Below image is of directories:下图是目录:

在此处输入图像描述

Anyone help me to fetch those 2 JSON data and calculate the array number to display on the dashboard.任何人都可以帮助我获取那些 2 JSON 数据并计算要显示在仪表板上的数组编号。 Below images show the output:下图显示了 output: 在此处输入图像描述

This is how I'd advice to import a json file:这就是我建议导入 json 文件的方式:

import React from 'react'

export default function App() {
  const [users,setUsers] = React.useState([{}])
  React.useEffect(()=>{
    fetch('data/users.json').then((res)=>res.json()).then((data)=>{
      setUsers(data)
     })
     
  },[])
  return (
    <div className="App">
      <h1>Users:{users.length}</h1>
    </div>
  );
}

this is the sample users.json file:这是示例users.json文件:

[
  {
    "id": 1,
    "name": "name"
  },
  {
    "id": 2,
    "name": "name 2"
  }
]

this is the structure of the main directory:这是主目录的结构:
在此处输入图像描述
Here is the code这是代码

you can try to import your JSON:您可以尝试导入您的 JSON:

import data from 'data/users.json'

Then make a length of the data然后制作一段数据

Object.keys(data).length

or use JSON.parse或者使用 JSON.parse

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

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