Ad
Making Text Urls Clickable In A Div
I'm trying to make url's clickable in my react application. My current approach is as follows.
render() {
function urlify(text) {
var urlRegex = /(https?:\/\/[^\s]+)/g;
return text.replace(urlRegex, function(url) {
return '<a target="_blank" rel="nofollow noreferrer" href="' + url + '">' + '</a>';
})
}
const headingAvailable = (
<span className="home_post_text">{urlify(postData.heading)}</span>
);
return (
<div className="home_post_sections sec2 unchange_div">{headingAvailable}</div>
);
}
But i cannot get this to work correctly.
For example :
if my text is something like this
this is a good song https://www.youtube.com/watch?v=i9wBXC3aZ_I&index=8&list=RDxuAH21DkJow
My text is converted to something like this
this is a good song <a target="_blank" rel="nofollow noreferrer" href="https://www.youtube.com/watch?v=i9wBXC3aZ_I&index=8&list=RDxuAH21DkJow"></a>
How can i fix this ?
Ad
Answer
React escapes html tags in strings by default, in order to prevent XSS security flaws.
You need to return an a
component, something like that:
render() {
function urlify(text) {
const urlRegex = /(https?:\/\/[^\s]+)/g;
return text.split(urlRegex)
.map(part => {
if(part.match(urlRegex)) {
return <a href={part}>{part}</a>;
}
return part;
});
}
const headingAvailable = (
<span className="home_post_text">{urlify(postData.heading)}</span>
);
return (
<div className="home_post_sections sec2 unchange_div">{headingAvailable}</div>
);
}
class Hello extends React.Component {
constructor(props) {
super(props);
this.text = 'this is a good song https://www.youtube.com/watch?v=i9wBXC3aZ_I&index=8&list=RDxuAH21DkJow';
}
urlify(text) {
const urlRegex = /(https?:\/\/[^\s]+)/g;
return text.split(urlRegex)
.map(part => {
if (part.match(urlRegex)) {
return <a href={part} key={part}> {part} </a>;
}
return part;
});
}
render() {
return <div> {this.urlify(this.text)} </div>;
}
}
ReactDOM.render( <
Hello name = "World" / > ,
document.getElementById('container')
);
<script src="https://unpkg.com/[email protected]/umd/react.development.js"></script>
<script src="https://unpkg.com/[email protected]/umd/react-dom.development.js"></script>
<div id="container">
<!-- This element's contents will be replaced with your component. -->
</div>
Ad
source: stackoverflow.com
Related Questions
- → How to update data attribute on Ajax complete
- → October CMS - Radio Button Ajax Click Twice in a Row Causes Content to disappear
- → Octobercms Component Unique id (Twig & Javascript)
- → Passing a JS var from AJAX response to Twig
- → Laravel {!! Form::open() !!} doesn't work within AngularJS
- → DropzoneJS & Laravel - Output form validation errors
- → Import statement and Babel
- → Uncaught TypeError: Cannot read property '__SECRET_DOM_DO_NOT_USE_OR_YOU_WILL_BE_FIRED' of undefined
- → React-router: Passing props to children
- → ListView.DataSource looping data for React Native
- → Can't test submit handler in React component
- → React + Flux - How to avoid global variable
- → Webpack, React & Babel, not rendering DOM
Ad