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

202
Visualizações
Passing a spread object as a prop on a conditional basis

I have a component which is currently taking in a object value as a prop which is being spread.

const primaryProps = {/* about 10 key/values in here */}

<Component
  {...primaryProps}
/>

This works as intended. But now I am try to add an OR to it and add another object which is spread. But this is throwing syntax errors. Is there a way around it? Thanks.

Tried following which doesn't work.

const primaryProps = {/* about 10 key/values in here. Can also be empty/null */}
const secondaryProps = {/* about 10 key/values in here */}

<Component
  {...primaryProps || ...secondaryProps}
/>


<Component
  `${primaryProps.join(",") || secondaryProps.join(",")}`
/>
about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

If you want to pass secondaryProps only when the primaryProps are null or undefined you can do it like that (but note that this will not work if the passed object is defined but empty):

<Component {...(primaryProps ?? secondaryProps)} />
about 4 years ago · Juan Pablo Isaza Relatório

0

I don't think this is possible to do inside the props of a component. Also it isn't clean. What would I suggest is take the condition out of the component.

const props = condition ? primaryProps : secondaryProps
<Component {...props} />
about 4 years ago · Juan Pablo Isaza Relatório

0

Your attempt:

<Component
  {...primaryProps || ...secondaryProps}
/>

Won't work because you don't need to spread the secondaryProps.

If primaryProps is NULL secondaryProps will be used with the first spread syntax


So remove the second ... and use:

<Component { ...primaryProps || secondaryProps } />;

Small example, toggle primary to see it in action

class Child extends React.Component {  
    render() {
        return Object.values(this.props);
    }
}

class Example extends React.Component {    
    render() {
        // const primary = { foo: 'primary' };
        const primary = null;
        const secondary  = { bar: 'secondary' };
        
        return <Child { ...primary || secondary } />;
    }
}

ReactDOM.render(<Example />, document.body);  
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script>

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