0

I'm trying to implement a chat system which consist of Smilies :) . i'm using react js and i want to check and replace the smiley symbols in the receiving string.

  return(<ListItem
           leftAvatar={<Avatar src="profile pic" />}
           primaryText="Name"
           secondaryText={
          <p>
         {item.message}
         </p>
          }
           secondaryTextLines={2}
          />
               );

the string i want to check is given by {item.message}. ive tried to replace the string with {item.message}.replace(/:)/g,'<img src="../../../img/smileys/smile53893.gif"/>');

but it doesn't work!

0

1 Answer 1

3

What you are doing there is that you are inserting the image tag as an string into the DOM. You need to create real react elements, and replace the :) with the <img/> elements.

Here is a simplified version your code. Made also a demo about this for you.

var texts = item.message.split(/:\)/g);
var content = [];
for(var i = 0; i < texts.length - 1; i++) {
  content.push(texts[i]);
  content.push(<img src='https://upload.wikimedia.org/wikipedia/commons/e/e2/Bye.gif'/>);
}
return <div>{content}</div>;
Sign up to request clarification or add additional context in comments.

2 Comments

thanks its working but i've a small issue when ever i pass a string without a smiley it doesn't return anything. and even if i pass a string containing a smiley the text after the smiley will not be displayed. please help.
@TRomesh did you ever figure out how to have it display the text without an emoji? Currently debugging the same issue. Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.