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

325
Visualizações
Variable created by const is not accessable before initialization in javaScript. But why there is no error in react create functional component
const BasicFunctionalComponent = props => {
   return (
      <div>
        {constantVariable}
      </div>
   )
}

export default BasicFunctionalComponent

const constantVariable = '**how I am initialized**?'

in example above I just create a basic functional component and after exporting it just initialized a constant variable set as a string. then I used it inside JSX above expected to go throw error 'Cannot access 'constantVariable' before initialization'. but all's good

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

0

Because BasicFunctionalComponent will get called much later. It's basically an asynchronous function from the perspective of this file you showed us. By the time that function is called, constantVariable will be defined.

about 4 years ago · Juan Pablo Isaza Relatório

0

Let's start with a much more clear example:

const fx = () => {
    console.log(v)
}

// fx() // Call it here and it throws error

const v = 1

// fx() // Call it here and it's ok

console.log('Fin!')

The reason behind why it does not throw ReferenceError is basically because the component function is not called before the initialization of the constantVariable.

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