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

271
Visualizações
Javascript Curly Braces Around Parameters

I'm learning react and javascript right now and am struggling to understand why curly braces are required when passing a simple string into a react functional component like so:

function Editor({ headerText }) {
       
    return (
        <div className="title">
            <span>{headerText}</span>
        </div>
    );
}

export default Editor;

I understand the concept of object destructuring. However, the headertext parameter is just receiving a basic string. Why do I get syntax errors when trying to remove the curly braces if headerText is just a basic string, so it shouldn't even need the curly braces?

In fact, the react docs have a function similar to this that doesn't even use curly braces:

function formatName(user) {
  return user.firstName + ' ' + user.lastName;
}

So why is it that my code snippet requires curly braces when the parameter is receiving a simple string value?

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

0

It requires the curly braces due to the fact that headerText is a property name on the props object. If you write it as <span>headerText</span> it will render headerText as a literal string rather than the value passed as props by <Editor headerText='text value' />
note: rendering a string without quotes is technically a feature of babel and not React but React comes with babel by default, I believe if you didn't use babel, it would just be invalid javascript

When you use <span>{headerText}</span> this is actually grabbing the variable headerText and rendering that value in the span tag.

Here is a very simple example of the difference in the two. Just uncomment the second return and comment the first to see them change.

https://codesandbox.io/s/friendly-babycat-rqzo9?file=/src/Test.js

about 4 years ago · Juan Pablo Isaza Relatório

0

Why curly braces are required when passing a simple string into a react functional component

This is by design.

Consider when Editor is invoked in another component:

function MyComponent() {
   return(
     <Editor headerText="Header"/>
   )
}

When React calls Editor function, it passes in an object, known as props.

That object will have the single string headerText and the value that was passed when invoked, however your component has no guarantee that the passed in props ONLY has that singular value.

Consider if your component is used like so

function MyAwesomerComponent() {
   return(
    <>
    {[1,2,3,4,5].map(i=>
      <Editor key={i} env="production" headerText="Header"/>
     )}
    </>
   )
}
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