9

I have component like this:

import React from 'react';
import Autolinker from 'autolinker';

class Comment extends React.Component{

    constructor(props){
        super(props);
    }

    render(){
        return <li className="media comment">
            <div className="image">
                <img src={this.props.activity.user.avatar.small_url} width="42" height="42" />
            </div>
            <div className="body">
                <p>
                    <strong>{this.props.activity.user.full_name}</strong>
                </p>
            </div>
            <div>
                <p>
                    {Autolinker.link(this.props.activity.text)}
                </p>
            </div>
        </li>;
    }
}

export default Comment;

Autolinker returns me a string value like this:

"So basically <a href="http://www.silastar.com/dev-sila" target="_blank">silastar.com/dev-sila</a> is perfect and works correctly?"

How to convert this string to html JSX so that anchor link would appear as link not as plain text??

Bergi
  • 630,263
  • 148
  • 957
  • 1,375
zazmaister
  • 667
  • 1
  • 7
  • 10

1 Answers1

8

You have to use dangerouslySetInnerHTML:

<p dangerouslySetInnerHTML={{__html: Autolinker.link(this.props.activity.text)}} />
GJK
  • 37,023
  • 8
  • 55
  • 74
  • 1
    This solution imposes XSS security threat if `this.props.activity.text` is not sanitized. The [common approach to sanitizing](http://stackoverflow.com/a/6234804/5763764) html characters unfortunately distorts some URLs for Autolinker. – Radek Matěj Feb 15 '17 at 10:10