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

107
Visualizações
How do I tell if a prop was passed in a react component

I have a react function component, This function takes in some props.

I want to know if the component had a specific prop passed to it.

Say I have a component, Song, that takes in 1 prop, Genre. I would want to know if the component when used was used with the genre prop.

examples:

  • genre not passed:
<Song></Song>
  • genre passed:
<Song genre="Rap"></Song>

edit: clarity

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

0

You can just check for the undefined value for the description.

for example:

const MyComponent = ({ title, description }) => {
    return (
        <div class ="card">
            <div class="card-header">{title}</div>
            {description && <div class="card-body">{description}</div>}
        </div>
    )
}

In above example I have used conditional rendering to meet your requirement.

about 4 years ago · Juan Pablo Isaza Relatório

0

maybe you must create the functional compontent that receive props from parent genre and the default props of genre is all, maybe the component will look like this:

const Song = ({ genre="all" }) => {
   if(genre === "all") {
     return(
        <div>this song list has a all genre</div>
     )
   } else if(genre === "Rap") {
     return(
        <div>this song list has a genre {genre}</div>
     )
   }
}

if we set default props so when from the parent component did not send props genre the default props of genre in song (which is "all") component will be applied.

about 4 years ago · Juan Pablo Isaza Relatório

0

put this in your function component return statement

genre can be switched for any prop, actually anything that returns true, false, 1, or 0

{props.genre != undefined && // if genre is not(!=) undefined, which happens when you dont pass the genre prop when using the component, it will run the code. the && means AND, which is true when the left operand is true and the right operand is true (the JSX returns true, i wont get into why)
 <Card.Subtitle>
   {props.genre}
 </Card.Subtitle>
}

edit: tried to add some clarity

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