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

190
Vistas
How to create a text component where first word of sentence to be bold?

I'm trying to create a text component where first word of the sentence to be bold. Right now with my solution, when a user enter "Tips: favourite vacation", I get " Tips: favouritevacation", where rest of sentence becomes mess and no space is created after Tips:. This isn't elegant solution. I thought about creating another component like TextBold and use it like this < Text >< TextBold >Tips:< /TextBold > Your Vacation< /Text >, but this seems unnecessary. Is there a way to get this working within Text component alone? https://codesandbox.io/s/text-component-v2dbt?file=/src/App.tsx

import * as React from "react";

export interface TextProps {
  children?: string;
}

export const Text: React.FunctionComponent<TextProps> = ({
  children
}: TextProps) => {
  return (
    <>
      <span style={{ fontWeight: "bold" }}>{children?.split(" ")[0]}</span>
      <span>{children?.split(" ").slice(1)}</span>
    </>
  );
};

export default function App() {
  return (
    <div>
      <Text>Tips: favourite vacation</Text>
    </div>
  );
}
about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

You could make use of first space char index and .substring

export const Text: React.FunctionComponent<TextProps> = ({
  children = ""
}: TextProps) => {
  const firstSpaceIndex = children.indexOf(" ");
  return (
    <>
      <span style={{ fontWeight: "bold" }}>
        {children.substring(0, firstSpaceIndex)}
      </span>
      <span>{children.substring(firstSpaceIndex)}</span>
    </>
  );
};

Edit text-component (forked)

about 4 years ago · Juan Pablo Isaza Denunciar

0

Try

<span style={{ fontWeight: "bold" }}>{children?.shift()}</span>
<span>{children?.split("").slice(1)}</span>
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