T
T
toster1112020-09-08 13:26:16
JavaScript
toster111, 2020-09-08 13:26:16

How to correctly insert an html tag into a string so that it is rendered as a dom element?

Hello!
You need to find a link in a line of text and make it clickable, that is, in fact, cut out the text of the link and replace it with a tag wrote such a function<a href='ссылка'>ссылка</a>

getTitle(title = 'Подробнее по ссылке: https://yandex.ru бла бла') {
    const regExp = /[[email protected]:%_\+.~#?&\/=]{2,256}\.[a-z]{2,4}\b(\/[[email protected]:%_\+.~#?&\/=]*)?/;

    if (title.match(regExp)) {
      const url = title.match(regExp)[0];
      const link = <a href={url}>{url}</a>;

      return title.replace(url, link);
    } else return title;
}


and as a result I get the following line: More at the link: [object Object] blah blah
I understand that the tag is an object, that's why it happens, but I don't understand how to correctly insert it so that it is rendered as a normal dom element

Answer the question

In order to leave comments, you need to log in

[[+comments_count]] answer(s)
0
0xD34F, 2020-09-08
@toster111

It is dangerouslySetInnerHTML to use. For example .
But in general, this is not necessary here .

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question