Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

163
Vistas
React: detect if children prop is JSX or a function returning JSX

I want to make a component that can either have JSX or a function that returns JSX as children. when I log the typeof the children prop for the function or JSX, it's both object. How do I detect if the children prop is a function or JSX?

This is what I have so far

type Props = {
    children: ReactNode | ((close: () => void) => ReactNode);
};

const Comp = ({ children }: Props) => (
    {typeof children === 'function' ? children() : children}
)
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

You condition is fine and you just need to wrap in div. When JSX is transpiled, it will be type of object

const Comp = ({ children }: any) => {
  console.log('***', typeof children);
  return <div>{typeof children === 'function' ? children() : children}</div>;
};

const Element = () => {
  return <div>
  <Comp>{() => <div>Hello</div>}</Comp>
  <Comp><div>Foo</div></Comp>
  </div>
}

const domContainer = document.querySelector('#app');
ReactDOM.render(<Element/>, domContainer);
<script crossorigin src="https://unpkg.com/react@16/umd/react.production.min.js"></script>
<script crossorigin src="https://unpkg.com/react-dom@16/umd/react-dom.production.min.js"></script>

<div id="app"> </div>

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda