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

197
Visualizações
Problem with import json to variable in React

I'm new in React and I'm trying to import and save in variable a selected JSON data

[...]

const languageToSet = "polish";
const lang = {
    promiseToSetLanguage: function(lang){
        return new Promise((resolve, reject) => {
            import language from `./languages/${languageToSet}_lang.json`
            language = JSON.parse(language)
            if (language.welcome) {
                console.log('Works fine')
                resolve(true)
            } else{
                reject(false)
            }
        })
    }
}
lang.promiseToSetLanguage()

[...]

Console throw me a error: "'import' and 'export' may only appear at the top level" but i need to load a JSON file selected by user. I used 'import' and 'require' method.

Thanks for your help, best regards.

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

I think that fetch might be the answer here.. Try to use

return new Promise(async (resolve, reject) => {
            const rawJsonFile = await fetch(`${file_from_user}`);
            const jsonFile = await rawJsonFile.json();
            if (jsonFile.welcome) {
                console.log('Works fine')
                resolve(true)
            } else{
                reject(false)
            }
        })

If it still wont work, you can always use xhr

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