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

106
Visualizações
Invalid JSON import in React

i got this error while trying import data from JSON file in the App component below:

Uncaught TypeError: start is undefined

import './App.css';
import PreNavbar from './Components/PreNavbar';
import Navbar from "./Components/Navbar.js";
import {BrowserRouter as Router} from "react-router-dom";
import Slider from "./Components/Slider.js";
import banner from "./data/data.json";

function App( ) {
  return (
    

    <Router>

      <PreNavbar />
      <Navbar />
      <Slider start = {banner.start} />

    
    </Router>
  );
}

export default App;

Here is the Slider component:

import React from 'react';
import Carousel from 'react-bootstrap/Carousel';

const Slider = ({start}) => {
  return (
    <Carousel>

      {start.map((item, index) => (

        <Carousel.Item>
          <img className='d-block w-100' src={item}
            alt="First slide" />

      </Carousel.Item>

      ))}


    </Carousel>
  )
}

export default Slider;

If i use {} in banner export, like so:

import {banner} from "./data/data.json";

I face this other error:

ERROR in ./src/App.js
Should not import the named export 'banner'.'start' (imported as 'banner') from default-exporting module (only default export is available soon)

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

0

Try passing a default value to your start prop on the <Slider /> component such as:

import React from 'react'
import Carousel from 'react-bootstrap/Carousel'

const Slider = ({start = []}) => { 
  return (
    <Carousel>
      {start.map((item, index) => (
         <Carousel.Item>
           <img className='d-block w-100' key={item} src={item} alt="First slide" />
         </Carousel.Item>
      ))}
    </Carousel>
) }

export default Slider;

Or you can add optional chaining like:

import React from 'react'
import Carousel from 'react-bootstrap/Carousel'

const Slider = ({start}) => { 
  return (
    <Carousel>
      {start?.map((item, index) => (
         <Carousel.Item>
           <img className='d-block w-100' key={item} src={item} alt="First slide" />
         </Carousel.Item>
      ))}
    </Carousel>
) }

export default Slider;

Or both:

import React from 'react'
import Carousel from 'react-bootstrap/Carousel'

const Slider = ({start = []}) => { 
  return (
    <Carousel>
      {start?.map((item, index) => (
         <Carousel.Item>
           <img className='d-block w-100' key={item} src={item} alt="First slide" />
         </Carousel.Item>
      ))}
    </Carousel>
) }

export default Slider;

I think the latter is the best approach to solving this issue. This may be happening because the app is still loading the JSON file data and when you are mounting the component, and since it expects the start prop to be loaded from the start, instead of being an undefined or null value, it throws an error.

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