Skip to content

Commit cb815c4

Browse files
committed
Update example
1 parent 7867d67 commit cb815c4

File tree

1 file changed

+28
-6
lines changed

1 file changed

+28
-6
lines changed

example/index.js

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,40 @@ import React, { PropTypes } from 'react';
22
import { render } from 'react-dom';
33
import reactReplaceString from 'react-string-replace';
44

5-
const HighlightNumbers = React.createClass({
5+
const Demo = React.createClass({
66
propTypes: {
77
content: PropTypes.string.isRequired,
88
},
99

10+
/**
11+
* NOTE: In many React examples you will see the `i` or `index` variable used
12+
* as the key for JSX tags (such as the `<a>` tags in this example), however
13+
* in this case we are iterating in three separate loops. This menas that we
14+
* cannot use `key={i}` because all three JSX tags could get the same key.
15+
*/
1016
render() {
17+
const text = 'Hey @ian_989, check out this link https://github.com/iansinnott/ Hope to see you at #reactconf';
18+
let replacedText;
19+
20+
// Match URLs
21+
replacedText = reactReplaceString(text, /(https?:\/\/\S+)/g, match => (
22+
<a key={match} href={match}>{match}</a>
23+
));
24+
25+
// Match @-mentions
26+
replacedText = reactReplaceString(replacedText, /@(\w+)/g, match => (
27+
<a key={match} href={`https://twitter.com/${match}`}>@{match}</a>
28+
));
29+
30+
// Match hashtags
31+
replacedText = reactReplaceString(replacedText, /#(\w+)/g, match => (
32+
<a key={match} href={`https://twitter.com/hashtag/${match}`}>#{match}</a>
33+
));
34+
1135
return (
1236
<div>
13-
<h1>Highlight Numbers</h1>
14-
{reactReplaceString(this.props.content, /(\d+)/g, (match, i) => (
15-
<span key={i} style={{ color: 'red' }}>{match}</span>
16-
))}
37+
<h1>React String Replace Demo</h1>
38+
{replacedText}
1739
</div>
1840
);
1941
},
@@ -22,4 +44,4 @@ const HighlightNumbers = React.createClass({
2244
const content = 'Hey my number is 555-555-5555.';
2345

2446
// Render the app
25-
render(<HighlightNumbers content={content} />, document.getElementById('root'));
47+
render(<Demo content={content} />, document.getElementById('root'));

0 commit comments

Comments
 (0)