Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
173
rated 0 times [  176] [ 3]  / answers: 1 / hits: 20577  / 9 Years ago, sun, january 24, 2016, 12:00:00

Given a react component like below (https://jsfiddle.net/69z2wepo/28684/):



var Hello = React.createClass({
render: function() {
let { cond, name } = this.props;
let content, content2;
if (cond){
content = (<div>Hello { name } </div>);
content2 = (<div>content 2</div>);
}

return (
<div>
{ content }
{ content2 }
</div>
)
}
});

ReactDOM.render(
<Hello name=World cond={ true } />,
document.getElementById('container')
);


How can I achieve something like:



content += (<div>content 2</div>) //not working


and then render the content in a single content variable instead of both content and content 2?


More From » reactjs

 Answers
3
var Hello = React.createClass({
render: function() {
let { cond, name } = this.props;
let content, content2, contents;
if (cond){
content = (<div>Hello { name } </div>);
content2 = (<div>content 2</div>);
contents = [content, content2];
}

return (
<div>
{ contents }
</div>
)
}
});

ReactDOM.render(
<Hello name=World cond={ true } />,
document.getElementById('container')
);


https://jsfiddle.net/69z2wepo/28687/


[#63576] Friday, January 22, 2016, 9 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
nikokorbinm

Total Points: 744
Total Questions: 126
Total Answers: 104

Location: Jersey
Member since Fri, Oct 1, 2021
3 Years ago
;