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

157
Visualizações
Is there a way to pass an imported component through a function?

I'm using a library called SlidingPane, and each pane has the exact same styles, just different content depending on the page. So I want to put it into a separate page, and get the content through an argument:

export default function CustomSlidingPane(content){
    <SlidingPane
        className="slidingPanel"
        isOpen={state.isPaneOpen}
        onRequestClose={() => {
            setState({ isPaneOpen: false });
        }}
        >
        <Container>
            {content}
        </Container>
    </SlidingPane>
}

All of the content is in separate .js pages that kind of look like this:

export default function One() {
    return (
        <h1>Hello</h1>
    );
}

So what I'm trying to do is pass one of the content functions through to the CustomSlidingPane like this:

import One from './One';
import CustomSlidingPane from './CustomSlidingPane';

export default function Main(){
    <Container>
        <CustomSlidingPane content={<One/>}>
    </Container>
}

Every thing I've tried always ends up in content being undefined. Is there any proper way to do this / is this even possible?

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

0

A few things:

  1. It looks like you mighthave forgotten a return in CustomSlidingPane
  2. You need to destructure content from the props

Try this

export default function CustomSlidingPane({content}){
    return (
        <SlidingPane
        className="slidingPanel"
        isOpen={state.isPaneOpen}
        onRequestClose={() => {
            setState({ isPaneOpen: false });
        }}
        >
        <Container>
            {content}
        </Container>
    </SlidingPane>
    )
}

Also, this pattern might be supported better with the children prop.

about 4 years ago · Juan Pablo Isaza Relatório

0

You will need to use children prop here. I am adding an example with a simple div element.

export default function CustomSlidingPane(props) {
  return <div>{props.children}</div>;
}

Content will be same as you defined in your example:

export default function One() {
  return <h1>Hello</h1>;
}

This is how you can pass the content to your slidingPlane component:

export default function Main() {
  return (
    <div>
      <CustomSlidingPane>
        <One />
      </CustomSlidingPane>
    </div>
  );
}
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