javascript - What is the difference between owner and parent component in React.js - Stack Overflow

React 0.13 brings parent-based context instead of owner-based context.So, i can't quite understand

React 0.13 brings parent-based context instead of owner-based context.

So, i can't quite understand the difference between owner and parent ponents. Examples will be appreciated.

React 0.13 brings parent-based context instead of owner-based context.

So, i can't quite understand the difference between owner and parent ponents. Examples will be appreciated.

Share Improve this question edited Apr 21, 2015 at 22:27 tbodt 17k7 gold badges61 silver badges86 bronze badges asked Apr 16, 2015 at 10:15 Olim SaidovOlim Saidov 2,8441 gold badge27 silver badges32 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 12
var A = React.createClass({
    render() {
        return (
            <B>
                <C />
            </B>
        );
    }
});

In the above example, A is the owner of B and C, because A creates both of the ponents.

However, B is the parent of C because C is passed as child to B.

More information can be found in the documentation.

It's important to draw a distinction between the owner-ownee relationship and the parent-child relationship. The owner-ownee relationship is specific to React, while the parent-child relationship is simply the one you know and love from the DOM.

From the official documentation:

An owner is the ponent that sets the props of other ponents

Here an example where A is the owner of B:

var A = React.createClass({
  render: function() {
    return <B />;
  }
});

A is the owner of B because B is created in A's render function.

This is an example where A is the parent of B:

var A = React.createClass({
  render: function() {
    return <div>{this.props.children}</div>;
  }
});

var B = React.createClass({
  render: function() {
    return <span>B</span>;
  }
});

React.render(
  <A><B /></A>,
  document.getElementById('example')
);

In this example, A is the parent of B because A's props.children contains B. But A has no direct knowledge of that its the parent of B, its children could be any ponent.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1743761463a4502705.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信