简体   繁体   English

使用React.cloneElement()保留的引用

[英]Refs preserved with React.cloneElement()

I don't get the point of statements written in React official docs : 我不明白在React官方文档中写的声明:

cloneElement() cloneElement()

 React.cloneElement( element, [props], [...children] ) 

Clone and return a new React element using element as the starting point. 使用element作为起点克隆并返回一个新的React元素。 The resulting element will have the original element's props with the new props merged in shallowly. 结果元素将具有原始元素的道具,新道具以浅层方式合并。 New children will replace existing children. 新的孩子将取代现有的孩子。 key and ref from the original element will be preserved. key和ref将保留原始元素。

React.cloneElement() is almost equivalent to: React.cloneElement()几乎相当于:

 <element.type {...element.props} {...props}>{children}</element.type> 

However, it also preserves refs. 但是,它也保留了refs。 This means that if you get a child with a ref on it, you won't accidentally steal it from your ancestor. 这意味着,如果你的孩子有一个参考,你不会意外地从你的祖先窃取它。 You will get the same ref attached to your new element. 您将获得与新元素相同的参考。

What makes me confused me is the statement This means that if you get a child with a ref on it, you won't accidentally steal it from your ancestor. 让我困惑的是声明这意味着如果你有一个带有参考的孩子,你就不会意外地从你的祖先那里偷走它。 You will get the same ref attached to your new element. 您将获得与新元素相同的参考。

If I am understanding Okay, the ref that points to Child element in Parent component will be preserved even if the Parent gets cloned. 如果我理解正常,即使父克隆,也会保留指向父组件中的子元素的引用。 So after React.cloneElement(Parent) , there are two individual Parents(which have same deep values inside, including ref), and both have refs respectively, and those refs points to same single Child. 因此,在React.cloneElement(Parent) ,有两个单独的React.cloneElement(Parent) (内部具有相同的深度值,包括ref),并且两者都分别具有ref,并且那些refs指向同一个Child。 Am I corrent? 我是否正确?

Then what works with ancestor ? 那么祖先的作用是什么? What is the ancestor in this context? 在这种情况下,祖先是什么?

To give you can example on what the docs seem to Illustrate, lets consider a component App that renders a component Main and which has two children Home and Dashboard for which it specifies refs 为了举例说明文档看起来是什么样的,我们可以考虑一个组件App,它呈现一个组件Main ,它有两个孩子Home和Dashboard,它指定了refs

class App extends React.Component {
   constructor(props) {
      super(props);
      this.homeRef = React.createRef();
      this.dashboardRef = React.createRef();
   }
   render() {
      return (
          <Main>
               <Home ref={this.homeRef} key={'Home'}/>
               <Dashboard ref={this.dashboardRef} key={'Dashboard'}/>
          </Main>
      )         
   }
}

Now the main component clones the child elements to add a props onClick to its children, 现在主要组件克隆子元素以向其子元素添加道具onClick,

class Main extends React.Component {
   onClick = () => {}
   render() {
       return (
          <div>
              {/* Some content here */}
              {React.Children.map(this.props.children, child => {
                 return React.cloneElement(child, {onClick: this.onClick})
               })}
          </div>
       )
   }
}

Now when the Main component clones the children in its like 现在,当Main组件克隆其中的子项时

React.cloneElement(child, {onClick: this.onClick})

which in our case are the Home and Dashboard component, if cloneElement were to ignore the key and ref passed to them by the App component, the App component won't have access to these children that it rendered. 在我们的例子中是Home和Dashboard组件,如果cloneElement忽略了key和由App组件传递给它的ref ,则App组件将无法访问它呈现的这些子组件。 Hence React.cloneElement preserves the refs and keys passed to the elements even if they are cloned in child render function. 因此,React.cloneElement保留传递给元素的引用和键,即使它们是在子渲染函数中克隆的。

The component created by createdElement is cloned child, and what is used by <Main> is the cloned child, then what does ref in <App> point? 由createdElement创建的组件是克隆子组件, <Main>使用的组件是克隆子组件,那么<App>中的ref是什么意思? Original child, or cloned child? 原来的孩子,还是克隆的孩子?

Does the phrase accidentally steal means something like forbidden access 这句话是否意外窃取意味着禁止访问

Ref to an element only makes sense when the element is rendered in the DOM. 只有元素在DOM中呈现时才能引用元素。 In the above case the original element is not rendered but the cloned element is. 在上面的例子中,原始元素不会被渲染,但克隆元素是。 Had React not assigned the same ref to the cloned component which is rendered in the DOM, App component wouldn't be interacting with anything significant. 如果React没有为在DOM中呈现的克隆组件分配相同的引用,则App组件将不会与任何重要事件进行交互。 Also if at all you decide to render both original children and the cloned children, the ref will point to the cloned children. 此外,如果您决定渲染原始孩子和克隆儿童,那么裁判将指向克隆儿童。

Demo illustrating the above 演示说明以上内容

PS CloneElement is not used to clone components, rather than rendered JSX instances and is done mostly to add more props to the PS CloneElement不用于克隆组件,而不是渲染JSX实例,主要是为了添加更多的道具

children component rendered from elsewhere 从其他地方渲染的子组件

I hope the above example explains the scenario 我希望上面的例子解释了这个场景

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

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