Click here to Skip to main content
15,868,016 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I want to use the "strong" tag while updating the state. But instead, it is directly printing the "strong" tag and not text in bold. I want my text in the state to be visible like text but instead it is directly pasting those HTML tags in DOM.

const [text, setText] = useState("Type your text here");
  const handleWord = (e) => {
    let searched = "your";
    if (searched !== "") {
      let text2 = text;
      let re = new RegExp(searched,"gi"); // search for all instances
      let newText = text2.replace(re, `<stong>${searched}`);
      setText(newText);
    }


What I have tried:

I tried using dangerouslySetInnerHTML but it dint work or maybe I don't know how to properly use it. Suggest something.
Posted
Comments
GuyThiebaut 4-Apr-22 7:39am    
I think what is happening here is that REACT may be protecting you from injecting a script into your page.

One thing I will mention is that you have a typo with your replacement string being `<stong>` i.e. it's missing an "r".

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900