Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

164
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!