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

244
Visualizações
Can I store a component in a variable and push children to it after it has been created?

Lets say that I have this component:

const Test = ({ children, ...rest }) => {
    return <>{children}</>
};

export default Test;

I am wondering if it is possible to create a variable that holds the component like this:

const test = <Test></Test>;

And then loop over some data and push children to the test variable on every iteration.

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

0

if you don't have the data yet, then all you have to do is conditionally render your component when you do have the data.

{ data ? (<Test>{data.map(...)}</Test>) : <SomeOtherComponent /> /* or null */}

or

{ data ? <>{data.map((x) => <Test>{x}</Test>)}</> : <SomeOtherComponent /> /* or null */}

depending on what you want achieve, i didn't fully understand your question

i.e. if you have the data you need, render the component, rendering the children as you see fit, otherwise render some other component (or null, to render nothing)

about 4 years ago · Juan Pablo Isaza Relatório

0

Yeap, try that pattern:

const test = (children) => <Test>{children}</Test>;

and usage

<>
{[1,2,3].map(el=>test(el))}
</>

[Edited]

const TestComp = ({children}) => <Test>{children}</Test>;

<>
{[1,2,3].map(el=>(<TestComp>{el}</TestComp>))}
</>
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