Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

235
Visualizações
React how set count text value 0 for empty box

Right now my empty text box showing count 1. see the screenshot below:

enter image description here

I want to show 0 value for empty box. how to do that? here is my code:

export default function TextFrom(props) {
  const handelUpClick = () => {
    let newtext = text.toUpperCase();
    setText(newtext);
  };

  const handelLoClick = () => {
    let newtext = text.toLowerCase();
    setText(newtext);
  };

  const handleOnChange = (event) => {
    setText(event.target.value);
  };

  const [text, setText] = useState("");

  return (
    <>
    ..my html code
     <h1>your text summary</h1>
        <p>
          {text.split(" ").length} words and {text.length} character
       </p>
    <> 
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

The problem here is that split returns [""] when called on an empty string, and the length of that array is indeed 1. To counteract this, you can replace the {text.split(" ").length} part of your code with {text.trim() === '' ? 0 : text.split(" ").length} so that the case where the input is only whitespace is taken care of.

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda