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

168
Visualizações
How do I persuade babel to let me define a Javascript array of consts?

I'm building my first expo/react app. I keep getting an "Unexpected token" error message in App.js:

export default class App extends React.Component {
   const [message, setMessage] = useState("...");

the error being in the [ of the line beginning const.

As best I can tell, this is a babel issue: this syntax was introduced in ES2015. AFAICS, this should be resolvable by adding @babel/preset-env to babel.config.js thus:

module.exports = function(api) {
  api.cache(true);
  return {
      presets: [
          '@babel/react',
          '@babel/env',
      ],
      plugins: [
          '@babel/proposal-class-properties',
      ],
  };
};

Bundling succeeds, but the error remains! What am I missing?

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

0

The error is not because you are trying to use array de-structuring in general, it is because you are doing it inside the body of a class (where you can only declare properties).

React Hooks are only compatible with Function components anyway. You can't use them in Class components.

So you need to use Function Component syntax.

const App = () => {
   const [message, setMessage] = useState("...");
   // ...
}

export default App;

Further reading:

  • Classes in JavaScript (MDN)
  • Function and Class Components (React) (A brief summary)
  • React Component (API reference for the React component class definition)
about 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