If you want to replace part of string with tag in JSX? 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 below example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
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> ); } }); |
If you like this question & answer and want to contribute, then write your question & answer and email to freewebmentor[@]gmail.com. Your question and answer will appear on FreeWebMentor.com and help other developers.