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

333
Visualizações
how to replace text when a user type text

Hi I'm working on a user mention function now. I have a problem that I can't replace text

If I'm trying to find a user with "@" I can't find a user so that's why I replace text.

  const handleInputComment = (e: React.ChangeEvent<HTMLInputElement>) => {
    setCommentValue({ ...commentValue, comment: e.target.value });

    e.target.value.split(" ").map((text) => {
      if (text.startsWith("@")) {
        text.replace("@", "");
        setMentionSearchValue(text);
        console.log(mentionSearchValue);
        return setIsMentionModalOpen(true);
      }
      return setIsMentionModalOpen(false);
    });
  };

when I see the console.log what I will see like @asdfsdf @sadfasfsaf @asdfjaskfjfkjf

how to replace text? thanks for reading my question.

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

Do that instead:

  const handleInputComment = (e: React.ChangeEvent<HTMLInputElement>) => {
    setCommentValue({ ...commentValue, comment: e.target.value });

    e.target.value.split(" ").map((text) => {
      if (text.startsWith("@")) {
        setMentionSearchValue(text.replace("@", ""));
        console.log(mentionSearchValue);
        return setIsMentionModalOpen(true);
      }
      return setIsMentionModalOpen(false);
    });
  };

Replace returns the text replaced and it doesn't update the variable. To the text variable was not being updated.

about 4 years ago · Juan Pablo Isaza Relatório

0

Javascript String Replace function is immutable, hence it won't change the value of text itself, rather, it will return a new value. But the problem with replace is it will replace all the occurances, if you are interested to get rid of the first @ only, you can use slice method. Another issue is, you are printing the value of state right after calling setState i.e. setMentionSearchValue, but setState is an asynchronous function, so you won't see the new value in the console immediately after calling setState. If you want to print it's value after mentionSearchValue gets updated, you need to use useEffect hook.

const handleInputComment = (e: React.ChangeEvent<HTMLInputElement>) => {
  setCommentValue({ ...commentValue, comment: e.target.value });

  e.target.value.split(" ").map((text) => {
    if (text.startsWith("@")) {
      // text.replace("@", "");
      setMentionSearchValue(text.slice(1));
      // console.log(mentionSearchValue);
      return setIsMentionModalOpen(true);
    }
    return setIsMentionModalOpen(false);
  });
};
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