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

331
Views
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 answers
Answer question

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 Report

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