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

118
Views
How to fix type issue for conditional rendering?

I'm trying to implement simple conditional rendering where if prefixText prop has more than 0 string then render double span elements else render single span element. But on prefixText.length I get error message saying ", is expected". Also on Text, I get "missing properties for ReactElement<any, any>" type error. How can I fix this issue? https://codesandbox.io/s/text-component-v2dbt?file=/src/App.tsx:0-538

import * as React from "react";

export interface TextProps {
  children: string;
  prefixText?: string;
}

export const Text: React.FunctionComponent<TextProps> = ({
  children,
  prefixText
}: TextProps) => {

  return (
  { prefixText.length > 0 ?
    <span>
      <span style={{ fontWeight: "bold" }}>{prefixText} </span>
      <span>{children}</span>
    </span> :
    <span>{children}</span>
  }
  );
};

export default function App() {
  return (
    <div>
      <Text prefixText="Tips:">favourite vacation</Text>
    </div>
  );
}
about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

You have to wrap your return statement into React.Fragment.

It should look like this

return (
  <>  
    { prefixText.length > 0 ? (
    
      <span>
        <span style={{ fontWeight: "bold" }}>{prefixText} </span>
        <span>{children}</span>
      </span> 
    ) : (
      <span>{children}</span>
    )
  </>  
}
);

Or you can also get rid of return braces and create a construction like this:

return prefixText.length > 0 ? (
        <span>
          <span style={{ fontWeight: "bold" }}>{prefixText} </span>
          <span>{children}</span>
        </span>
      ) : (
        <span>{children}</span>
      )
about 4 years ago · Juan Pablo Isaza Report

0

Update your return block code to as follows:

return (
  prefixText ?
    <span>
      <span style={{ fontWeight: "bold" }}>{prefixText}</span>
      <span>{children}</span>
    </span> :
    <span>{children}</span>
);

Or update your component to the following:

export const Text: React.FunctionComponent<TextProps> = ({
  children,
  prefixText
}: TextProps) => (
  prefixText ?
    <span>
      <span style={{ fontWeight: "bold" }}>{prefixText} </span>
      <span>{children}</span>
    </span> :
    <span>{children}</span>
);
about 4 years ago · Juan Pablo Isaza Report

0

wrap your component with react.fragment and also check if prefixText exist in your condition because prefixText is a partial in your typescript interface

return (
    <React.Fragment>
      { prefixText && prefixText.length > 0 ?
      <span>
        <span style={{ fontWeight: "bold" }}>{prefixText} </span>
        <span>{children}</span>
      </span> :
      <span>{children}</span>
    }
    </React.Fragment>
  );
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!