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

266
Visualizações
object destructuring in react-create-app

In a file called index.js i have the following export:

export default {
  foo: true,
  bar: false
}

Later in file home.js i'm doing the following:

import { foo, bar } from './index'

console.log(foo, bar) -> undefined, undefined

If i import everything like:

import index from './index'

console.log(index) -> { foo: true, bar: false }

Can someone explain me why this is happening? I'm doing something wrong?

I'm using:

› create-react-app -V 1.0.3

over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

What you have there are named exports, not destructuring.

You have to export them as such, not as a default export:

// this will export all of the defined properties as individual
// named exports, which you can pick-and-choose when importing
// using a similar syntax to destructuring
const foo = true, bar = false;
export {
  foo,
  bar
}

// imported exports named 'foo' and 'bar'
import { foo, bar } from './my-file'.

If you specify a default export, then whatever follows the keyword default will be exported when you import without curly braces:

// this exports an object containing { foo, bar } as the default export
export default {
  foo: true,
  bar: false
}

// imported without {}
import objectContainingFooBar from './my-file';
over 4 years ago · Santiago Trujillo 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