admin管理员组文章数量:1208153
Given a react component like below (/):
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?
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?
Share Improve this question asked Jan 24, 2016 at 13:06 mysticmystic 1451 gold badge1 silver badge5 bronze badges1 Answer
Reset to default 20var 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/
本文标签: javascriptHow to append element to variable to be render in reactStack Overflow
版权声明:本文标题:javascript - How to append element to variable to be render in react - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1738692740a2107196.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论