When you pass a JSX element to replace()
as the second argument, that element is converted to a string because replace()
expects a string as a second argument. What you need to do is convert your string to an array of strings and JSX elements. So your result
variable should contain something like ['Lorem ', <div className="spacer"></div>, ' ipsum']
.
Something like this:
function flatMap(array, fn) {
var result = [];
for (var i = 0; i < array.length; i++) {
var mapping = fn(array[i]);
result = result.concat(mapping);
}
return result;
}
var Comp = React.createClass({
render: function () {
var result = 'Lorem : ipsum';
result = flatMap(result.split(':'), function (part) {
return [part, <div>spacer</div>];
});
// Remove the last spacer
result.pop();
return (
<div>
{result}
</div>
);
}
});
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…