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

105
Views
React control flow component with Typescript

I have a Control flow component with React that

  • renders its children when the condition is true,
  • renders null or a fallback if the condition is false

The component

interface Props {
    when: boolean
    fallback?: () => JSX.Element
    children: Children
}

export const Show = ({
    when,
    fallback,
    children
}: Props) => {


    if (!when)
        return <>{fallback?.() || null}</>

    return <>{children}</>
}

Without the component

if I do not use this component and use a simple binary operator, Typescript works great:

interface Props {
    value: {nested: string} | null
}

const SomeComponent =({value}:Props)=>(
    <div>
        {value && (
            <div>
                {value.nested}

                value is inferred the type "{nested: string}"
            </div>
        )}
    </div>

With the component

If I use the control flow component the type is not inferred and typescript gives an error:

interface Props {
    value: {nested: string} | null
}

const SomeComponent =({value}:Props)=>(
    <div>
        <Show when={!!value}>
            <div>
                {value?.nested}

                typeof value remains "{nested: string} | null", 
                therefore I need some conditional chaining
            </div>
        </Show>
    </div>

Any idea on how to make type inference work?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You can't do it in this way, because the code inside <Show></Show> is actually executed before Show gets a chance to prevent the div from rendering. Think what happens when JSX is transpiled to javascript:

React.createElement(
  Show, 
  {when: !!value},
  React.createElement(
    'div',
    {},
    `${value?.nested} typeof value remains "{nested: string} | null", therefore I need some conditional chaining`
  )
)

See the problem? If we rewrite it a bit:

const textContent = `${value?.nested} typeof value remains "{nested: string} | null", therefore I need some conditional chaining`
React.createElement(
  Show, 
  {when: !!value},
  React.createElement(
    'div',
    {},
    textContent
  )
)

These snippets are exactly the same, and in both of them value?.nested is evaluated before Show even renders. I can't recommend you any good solution here, maybe something like this:

interface Props<T> {
    value: T
    fallback?: () => JSX.Element
    children: (value: T) => ReactNode
}

export const CheckExists = <T extends any>({
    value,
    fallback,
    children
}: Props<T>) => {
    if (!value)
        return fallback ? fallback() : null
    return <>{children(value)}</>
}

<CheckExists value={value}>
  {existingValue => (
    <div>{existingValue.nested}</div>
  )}
</CheckExists>

But this is different interface and may not be suitable for you

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!