简体   繁体   English

React 组件无法从本地 JSON 文件中获取数据

[英]React component cant fetch data from local JSON file

The following code was working when the const url data was being used and passed in the fetch.当 const url 数据被使用并在提取中传递时,以下代码有效。 I created a Json local file with a different array passed in fetch and now it is not working.我创建了一个 Json 本地文件,其中包含一个传入 fetch 的不同数组,但现在它无法正常工作。 Should I re-phrase my code now that it is local and from a URL?既然我的代码是本地的并且来自 URL,我是否应该重新表述我的代码? The console says "Uncaught Error: Too many re-renders. React"控制台显示“未捕获的错误:重新渲染太多。反应”

import React, { useState } from 'react'
import { FaAngleDoubleRight } from 'react-icons/fa'
import data from './data.json'

function App() {
  const [jobs, setJobs] = useState(data)
  const [value, setValue] = useState(0)
  const newJobs = data
  setJobs(newJobs)
  
  const { company, dates, duties, title } = jobs[value]
  return (
    <section className="section">
      <div className="title">
        <h2>experience</h2>
        <div className="underline"></div>
      </div>
      <div className="jobs-center">
        {/* btn container */}
        <div className="btn-container">
          {jobs.map((item, index) => {
            return (
              <button
                key={item.id}
                onClick={() => setValue(index)}
                className={`job-btn ${index === value && 'active-btn'}`}
              >
                {item.company}
              </button>
            )
          })}
        </div>
        {/* job info */}
        <article className="job-info">
          <h3>{title}</h3>
          <h4>{company}</h4>
          <p className="job-date">{dates}</p>
          {duties.map((duty, index) => {
            return (
              <div key={index} className="job-desc">
                <FaAngleDoubleRight className="job-icon"></FaAngleDoubleRight>
                <p>{duty}</p>
              </div>
            )
          })}
        </article>
      </div>
      <button type="button" className="btn">
        more info
      </button>
    </section>
  )
}

export default App;

    enter code here

If data is really just JSON data:如果data真的只是 JSON 数据:

import data from './data.json'

Then this makes no sense:那么这就没有意义了:

const reponse = await fetch(data)

What are you fetching ?你在拿什么 The data that's already in data ?数据中已经存在的data This is most certainly failing and there's an error in your browser's development console.肯定是失败的,并且浏览器的开发控制台中存在错误。 ( Always check for errors first.) 始终首先检查错误。)

If you already have the data then you can remove the fetch operation, which means you can remove the useEffect entirely, and remove the loading state entirely.如果您已经有了数据,那么您可以删除fetch操作,这意味着您可以完全删除useEffect ,并完全删除loading state。 Just use data as the start of your state:只需使用data作为 state 的开始:

const [jobs, setJobs] = useState(data);

In fact, if you don't ever modify this data and it's always statically displayed then you don't even need the jobs state either.事实上,如果您从来不修改此数据并且它始终静态显示,那么您甚至不需要jobs state。 Just use data in your render.只需在渲染中使用data

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

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