I have a generic component which maps its child components to filter only children of a certain type, as found below.
However, using the property type
was a mere guess, and I can't find it documented. Not only that, logging it shows it being a function - which can't be executed. On top of that there's a couple issues that need to be worked around when using Browserify.
Another option would be to read child.prototype.displayName. But that too feels wrong.
Question: Basically, I'm looking for a solid way of comparing whether two ReactJS components are equal.
EXAMPLE
(Updated: not that bad after all)
var Foo = React.createClass({
render: function() {
return <div>Foo</div>;
}
});
var Bar = React.createClass({
render: function() {
return <div>Bar</div>;
}
});
var Main = React.createClass({
render: function() {
var filteredChildren = [];
filteredChildren = React.Children.map(function(child) {
if (child.type === Foo.type) {
return child;
}
});
return (
<div>
{filteredChildren}
</div>
);
}
});
React.render(<Main><Foo /><Bar /></Main>, document.body);
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…