Let’s explore how does the ownership work in React, and what is exactly a child in React’s world by looking at the special property : this.props.children
.
Luke, I’m your.. owner?
When you do:
var MyContainer = React.createClass({ render: function() { return} });
is the owner of
, and
is the ownee.
We don’t talk about parent/child, this is used to talk about the DOM only.
In this example:
var MyContainer = React.createClass({ render: function() { return} });
is not the parent of
, it’s only its owner.
Being the parent refers to the DOM hierarchy, thus in this case, it’s the You can see it through the React Chrome Developer Tools for instance: To resume, an owner can only be a React Component, an ownee can be anything (component or basic html tag). To find the owner of a node, you need to look for its closest React Component through its ancestors hierarchy, it’s the one that Now, let’s focus about the children, and especially the special property This is a property automatically set by React on your component that contains the children of the current component you are. Don’t use Here is a simple component that includes its children into a That renders: In this example, Often we can find self-closed tag when rendering components, for instance Meaning that a single object ReactElement: an array of ReactElement : a string : So, basically, 4 different types. You’re going to have some bloat code to handle all those cases. Or not. Hopefully, the React team had created some helpers to do the dirty job and make our code cleaner (Note: by default it’s not always an array to avoid useless allocation): Here is a small example that renders only children of type Here is the list of the children : Only the
and the parent of
. But let’s focus on the ownership.
We can see that :
does not have any owner
is
, not the
too).
is
render()
it (and sets its props if any).this.props.children in-depth
this.props.children
.children
as a name for your own props, you might have troubles (ie: it will be override if you really have children in your component).
var Parent = React.createClass({
render: function () {
return
this.props.children
corresponds to the content put inside the
where it was rendered. In the example, this.props.children
is a single ReactElement
(type span).this.props.children
is an Array of two ReactElement
of type span :
React.render(
return
, but this is just the case where this.props.children === undefined
(it’s the same if you do
).
this.props.children
can contains either :
undefined
:
or
or
Uncaught Error: Invariant Violation: onlyChild must be passed a children with exactly one child
(even if I have a string a child, I think it’s a bug, because .count returns 1), otherwise returns it
span
or MyComponent
:
var ShowOnlySpansOrMyComponent = React.createClass({
render: function() {
var onlySpans = React.Children.map(this.props.children, function(child) {
if (child.type === 'span' || child.type === MyComponent.type) {
return child;
}
});
return
" you have to " : no type
ReactElement : span
" " : no type
ReactElement : div
" " : no type
ReactElement : here, type is
function (props)
: this is the ReactElement constructor..
" " : no type
ReactElement : span
" " : no type
and
appear in the rendering.
To compare the type of components, notice that we used the value of MyComponent.type
which is the same constructor of ReactElement we are talking about.