简体   繁体   English

ReactJS-孩子如何找到其父母?

[英]ReactJS - How can a child find its parent?

Is there a way in ReactJS for a component to find out who it's parent is? 在ReactJS中,有没有一种方法可以让组件找出其父对象是谁?

EDIT 1: Regardless of the merits of doing this, is there a way? 编辑1:不管这样做有什么优点,有没有办法?

I haven't found a React way to do this - from what I can see, the idea is to pass callbacks down to the child from the parent, and the child calls the callback - unaware of the fact that the callback is actually on the parent. 我还没有找到一种执行此操作的React方法-从我的看到,想法是将回调从父级传递给子级,然后子级调用该回调-并未意识到该回调实际上位于父母

I've tried setting an "owner" property, and that idea seems to work, but I wonder what's the best approach? 我尝试设置一个“所有者”属性,这种想法似乎行得通,但是我不知道最好的方法是什么?

eg 例如

<Parent>
  <Child owner={this}/>
</Parent>

Then in the child component, I can do owner.method , and it seems to work fine. 然后在子组件中,我可以执行owner.method ,并且看起来工作正常。 I know this isn't a true parent/child relationship, but is the closest I've found in my tests. 我知道这不是真正的父母/孩子关系,但是这是我在测试中发现的最接近的关系。

Some may say that callbacks are a cleaner way of doing this, but the parent/child relationship for some things (eg RadioButtonGroup and RadioButton) seems natural and would benefit from knowing this relationship, in my opinion. 有人可能会说回调是一种更干净的方法,但是在某些情况下,父/子关系(例如RadioButtonGroup和RadioButton)看起来很自然,并且我认为了解这种关系会有所帮助。

EDIT 2: So it's not possible? 编辑2:因此不可能吗?

The thing that I don't like about the idea that it's not supported is that HTML can be marked up with zero javascript - and it has implied, default functionality - some elements are required to have parents - they are defined as children of other elements (eg ul and li). 我不喜欢不支持HTML的想法是,HTML可以用零个javascript标记-它暗示了默认功能-某些元素必须具有父级-它们被定义为其他元素的子级(例如ul和li)。 This can't happen in JSX because if there is interaction between the elements - there has to be javascript events that bind the components together - every single time you use them. 这不能在JSX发生,因为如果有要素之间的相互作用-必须 JavaScript事件是绑定组件一起-你使用它们每一次。 Designers can't simply write HTML like syntax - Someone has to step in and put some javascript bindings in there - which then makes the maintenance harder. 设计人员不能简单地编写类似语法的HTML-有人必须介入并在其中放置一些javascript绑定-这样会使维护工作变得更加困难。 I think the idea makes sense for overriding default behavior, but default behaviors should be supported. 我认为该想法对于覆盖默认行为是有意义的,但是应该支持默认行为。 And defaults would require knowing either your parent, or who your owner is. 违约将需要了解您的父母或所有者。

There are a number of benefits to not doing this, the main two are: reusability and encapsulation. 不这样做有很多好处,主要的两个是:可重用性和封装性。

TL;DR you probably don't want to do this ever. TL; DR您可能永远不想这样做。

Let's say our RadioButton has this public interface: 假设我们的RadioButton具有以下公共接口:

<RadioButton active={true} onSelect={function(event){}}>text</RadioButton>

We could construct another component called SuperRadioButton, which might have a different way of presenting itself, but still have the same public api as RadioButton, so it's a valid child of RadioButtonGroup. 我们可以构造另一个名为SuperRadioButton的组件,该组件可能具有不同的表示方式,但仍具有与RadioButton相同的公共api,因此它是RadioButtonGroup的有效子代。

If we're accessing the parent, then the parent's internals become part of the public api of these components, and we need to be much more careful with how we change our code, because a change in any of these components could cause the entire application to break. 如果我们正在访问父级,那么父级的内部将成为这些组件的公共api的一部分,并且我们需要更加谨慎地更改代码,因为任何这些组件的更改都可能导致整个应用程序打破。

Callbacks. 回调。 Owner properties. 所有者属性。 Passing events out to be caught by the root in the tree. 将事件传递出去,以被树中的根捕获。 Passing the root down through contexts. 通过上下文传递根源。

There are ways, yes, but they're contrary to the conceptual model of react, which is to be explicitly top down at all times. 有很多方法,是的,但是它们与反应的概念模型相反,后者始终始终是自上而下的。 The short version is "you can, but don't." 简短的版本是“可以,但是不能。”

The fundamental problem is that you don't want a child mutating outside its parent's knowledge. 根本的问题是您不希望孩子在父母的知识之外进行变异。

That means that the sole exception to this is the root of the component tree, so it's semi-legit to pass a member of that control downwards in props or contexts then to "pass things up" by telling the root, which may then repaint itself. 这意味着唯一的例外是组件树的根,因此在属性或上下文中向下传递该控件的成员,然后通过告诉根“向上传递”,这是半合法的,然后可以重新绘制自身。

The application layer Flux does something not terribly dissimilar to this, but passes things outside of the component heirarchy entirely to a dataStore, which broadcasts things back in with events. 应用程序层Flux所做的并非与此完全不同,但是将整个组件层次结构之外的内容完全传递给了一个dataStore,后者将事件随事件广播回去。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM