Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

202
Visualizações
How to load data from multiple json files in react

I have 10 json files in a directory inside react app. I need to search data and matching data could be any of the file. For this do i need toI load all data of each json file in the react state. If yes how to load all data of each json files in the react state.

If there any better way to do this please suggest me.

Directory structure like this:

 students/
    /level1.json
    /level2.json
    /level3.json
    /more files......
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Here is the sample snippet

  //Have your static jsons inside public folder, So that after compilation react able to get the static files
  const [fileData,setFileData] = useState([])
  useEffect(()=>{
    const fileList = ['level1' ,'level2' ,'level3']; //files list in public folder
    fileList.forEach(filename => {
      fetch(`./${filename}.json`).then(response => {
        return response.json() //parse json
      }).then(data => {
        setFileData(files => [...files, {[filename]:data}]); // pushing json data by key of filename
      } 
        )
    })
  },[])

fetch API will look for files like -> "http://localhost:3000/level1.json" which is actually the public directory, So if you use fetch. You need to move it under public folder.

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda