admin管理员组文章数量:1355522
In react you can do something like:
var Wrap = React.createClass({
render: function() {
return <div>{ this.props.children }</div>;
}
});
var App = React.createClass({
render: function() {
return <Wrap><h1>Hello word</h1></Wrap>;
}
});
This allows you to pass in a ponent to another. But what if Wrap had another div that you could put content into. So consider the following:
var Wrap = React.createClass({
render: function() {
return (
<div>
<ul className="icons">
// Another poent should be passed here from App to render icons.
</ul>
{ this.props.children }
</div>
);
}
});
var App = React.createClass({
render: function() {
return <Wrap><h1>Hello word</h1></Wrap>;
}
});
In the above example you can see that not only do I want to pass in children of the App
ponent but I also want to pass another ponent that is icons for the ul
section. Is this possible?
If so, how?
In react you can do something like:
var Wrap = React.createClass({
render: function() {
return <div>{ this.props.children }</div>;
}
});
var App = React.createClass({
render: function() {
return <Wrap><h1>Hello word</h1></Wrap>;
}
});
This allows you to pass in a ponent to another. But what if Wrap had another div that you could put content into. So consider the following:
var Wrap = React.createClass({
render: function() {
return (
<div>
<ul className="icons">
// Another poent should be passed here from App to render icons.
</ul>
{ this.props.children }
</div>
);
}
});
var App = React.createClass({
render: function() {
return <Wrap><h1>Hello word</h1></Wrap>;
}
});
In the above example you can see that not only do I want to pass in children of the App
ponent but I also want to pass another ponent that is icons for the ul
section. Is this possible?
If so, how?
Share Improve this question edited Oct 9, 2014 at 15:19 phtrivier 13.4k9 gold badges52 silver badges82 bronze badges asked Oct 9, 2014 at 3:14 SeekingTruthSeekingTruth 1,0543 gold badges15 silver badges23 bronze badges 1-
Aren't these two different ponents?
<Icons />
, which returns the<ul>
, and whatever goes inside{ this.props.children}
. Then inApp
you can instantiate two different ponents rather than one. I think you'll continue to feel like you're fighting React if you pass ponents as props. – Ross Allen Commented Oct 9, 2014 at 22:37
2 Answers
Reset to default 4I see two options:
Pass the other ponent as prop, e.g.
<Wrap icons={<Icons />}><h1>...</h1></Wrap>
Pass two children two
Wrap
and render each of them in the appropriate places, e.g.<Wrap> <Icons /> <h1>...</h1> </Wrap>
Using Higher order ponents to do this is cleaner than accessing children by index or looping imo.
const wrap = (Icons) => class extends React.Component {
render() {
return (
<div>
<ul className="icons">
<Icons {...this.props.iconProps} />
</ul>
{ this.props.children }
</div>
);
}
});
class AppIcons extends React.Component {
render: function() {
return <div />
}
});
class App extends React.Component {
render: function() {
return <Wrap iconProps={this.props}><h1>Hello word</h1></Wrap>;
}
});
const Wrap = wrap(AppIcons);
ReactDOM.render(App, domContainerNode)
本文标签: javascriptrendering multiple children of different types in reactStack Overflow
版权声明:本文标题:javascript - rendering multiple children of different types in react - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744046159a2581560.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论