Skip to content

Instantly share code, notes, and snippets.

@peterjmag
Created August 30, 2015 21:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save peterjmag/1d5b0d6aaa53049142d3 to your computer and use it in GitHub Desktop.
Save peterjmag/1d5b0d6aaa53049142d3 to your computer and use it in GitHub Desktop.
TextWithLineBreaks React component
var _ = require('lodash');
var React = require('react');
module.exports = React.createClass({
displayName: 'TextWithLineBreaks',
render: function () {
var nodes = [];
var fragments = this.props.text.split('\n');
var key = 0;
_.each(fragments, function (fragment) {
nodes.push(
<span key={key}>{fragment}</span>
);
key++;
nodes.push(
<br key={key} />
);
key++;
});
// Remove extra last <br /> tag.
nodes.pop();
return (
<span className="text-with-line-breaks">
{nodes}
</span>
);
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment