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

123
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar

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 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