简体   繁体   English

使用this.props.children将道具传递给第一个孩子

[英]Pass props to the first child using this.props.children

I know the way we pass props to children when using this.props.children the following way: 我知道以下方式使用this.props.children时将道具传递给孩子的方式:

React.Children.map(this.props.children, (child: any, index: number) => {
    return React.cloneElement(child, {
                Visibility: this.state.visibility,
        });
});

To pass prop to only the first child I can use the index and wrap the cloneElement call in an if (index === 0) , but this won't stop map from interating over all the children. 要将prop只传递给第一个孩子,我可以使用index并将cloneElement调用包装在if (index === 0) ,但这不会阻止map遍历所有孩子。

I'm wondering if there's a way I can break the loop right after index 0 is reached. 我想知道是否有一种方法可以在到达索引0后立即中断循环。 Thanks! 谢谢!

I'm wondering if there's a way I can break the loop right after index 0 is reached. 我想知道是否有一种方法可以在到达索引0后立即中断循环。 Thanks! 谢谢!

Don't use map at all, then, if you only want the first child. 如果只想要第一个孩子,则根本不使用map

const child = (Array.isArray(this.props.children) ? this.props.children[0] : this.props.children) || null;
return child && React.cloneElement(
    child,
    {Visibility: this.state.visibility }
);

That first bit: 第一点:

const child = (Array.isArray(this.props.children) ? this.props.children[0] : this.props.children) || null;

...guards against the fact that this.props.children is only an array when there are multiple children ( details ); ...防止以下事实:只有多个子对象时, this.props.children才是数组( 详细信息 ); otherwise, it's the single child passed, or undefined if no children are passed (which we convert to null so we can directly return it from render ). 否则,它是传递的单个子对象;如果未传递任何子对象,则为undefined (我们将其转换为null以便我们可以直接从render返回它)。

Then the return child && guards aginst the possibility there were no children at all. 然后, return child &&守卫们可能根本没有孩子。

Example with all those situations: 所有这些情况的示例:

 class Example extends React.Component { constructor(...args) { super(...args); this.state = { visibility: true }; } render() { const child = (Array.isArray(this.props.children) ? this.props.children[0] : this.props.children) || null; return child && React.cloneElement( child, {Visibility: String(this.state.visibility) } ); } } const Child = props => <div>Visibility: {props.Visibility}</div>; ReactDOM.render( <div> <Example /> <Example><Child/></Example> <Example><Child/><Child/></Example> </div>, document.getElementById("root") ); 
 <div id="root"></div> <script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script> 

Since you only need the the first item, there is no need to loop through them. 由于您只需要第一项,因此不需要遍历它们。

React.Children.toArray() will always give you an array, regardless if you have one or multiple children, which saves you some of the checks in TJ's answer. 无论您有一个还是多个孩子, React.Children.toArray()都会始终为您提供一个数组,这为您节省了TJ答案中的某些检查。

So here is a slightly shorter (and imo cleaner) solution: 因此,这是一个略短(和imo清洁器)的解决方案:

  const child = React.Children.toArray(children)[0];
  if (child) {
    return React.cloneElement(child, {Visibility: this.state.visibility});
  }
  return null;

React.Children.toArray(children)

Returns the children opaque data structure as a flat array with keys assigned to each child. 以分配给每个子项的键的平面数组形式返回子项不透明数据结构。 Useful if you want to manipulate collections of children in your render methods, especially if you want to reorder or slice this.props.children before passing it down. 如果要在渲染方法中操纵子级集合,则很有用,特别是如果要在传递它之前对this.props.children重新排序或切片。

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

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