简体   繁体   English

React props.children 不是数组

[英]React props.children is not array

According to the react docs , if a component has multiple children, this.props.children should be an array.根据react 文档,如果一个组件有多个子组件,则 this.props.children 应该是一个数组。

I have the following component:我有以下组件:

export class Two extends React.Component {

    componentDidMount() {
        console.log(Array.isArray(this.props.children)); // false
    }

    render() {

        return(
            <div>
                {this.props.children}
            </div>
        );
    }

};

Which I pass children to in another component's render() method:我在另一个组件的 render() 方法中将孩子传递给它:

<Two>
    <Img src="/photos/tomato.jpg"/>
    <Img src="/photos/tomato.jpg"/>
</Two>

Why is this.props.children not an array?为什么 this.props.children 不是数组? More importantly, how can I get it to be one?更重要的是,我怎样才能让它成为一个?

Found a better solution to this after some digging in the React.Children source .在对React.Children 源代码进行一些挖掘后找到了一个更好的解决方案。 It looks like a .toArray() method has been added in React 0.14, soon to be released.看起来.toArray()方法已在 React 0.14 中添加,即将发布。

Once it is out we will be able to simply do something like this:一旦它出来,我们将能够简单地做这样的事情:

let children = React.Children.toArray(this.props.children);

It's documented in https://reactjs.org/docs/react-api.html#reactchildren它记录在https://reactjs.org/docs/react-api.html#reactchildren

I found this solution.我找到了这个解决方案。 It will render all children, one or more.它将渲染所有孩子,一个或多个。

const BigMama = ({ children, styles, className }) => {
  return (
    <div
      styles={{styles}}
      className={(className ? className : '')}
    >
    {
      React.Children.map(children, (child) =>
        <React.Fragment>{child}</React.Fragment>)
    }
  </div>)
}


<BigMama
  styles={{border: 'solid groove'}}
  className='bass-player'
>
  <h1>Foo</h1>
  <h2>Bar</h2>
  <h3>Baz</h3>
  <h4>Impossibru!</h4>
<BigMama>

There are many ways and some are mentioned above already but if you wanna keep it simple and want to achieve this without any utility then below should work有很多方法,上面已经提到了一些方法,但是如果你想保持简单并想在没有任何实用程序的情况下实现这一点,那么下面应该可以工作

const content = [
  <Img src="/photos/tomato.jpg"/>,
  <Img src="/photos/tomato.jpg"/>
];

<Two>
  {content}
</Two>

You might find the spread syntax useful in this case.在这种情况下,您可能会发现传播语法很有用。 Have you tried it out?你试过了吗?

<div>
    { [...this.props.children] }
</div>

Combine with map to manipulate the output.结合 map 来操作输出。

<div>
    { [...this.props.children].map(obj => <div style="someStyling"> {obj} </div> ) }
</div>

If you want to do something with the results (eg. render the array in a list), this is how you can use children.如果你想对结果做一些事情(例如,在列表中渲染数组),这就是你可以使用孩子的方式。 The React docs talk about how the map function works with children, as it's an opaque structure. React 文档讨论了map函数如何与孩子一起工作,因为它是一个不透明的结构。

export const List = ({ children }) => {
  return (
    <ul>
      {React.Children.map(children, child => <li>{child}</li>)}
    </ul>
  )
}

So then you can call那么你可以打电话

<List>
  {item.name}
  {item.price}
</List>

Is it because is it a DOM node?是因为它是DOM节点吗? Try console.log(this.props.children) you will notice that it logged a array of objects (note that each object contains information of the child element of the component).尝试console.log(this.props.children)你会注意到它记录了一个对象数组(注意每个 object 包含组件的子元素的信息)。 I read that node and array are not the same tho they have a same format.我读到节点和数组不一样,但它们具有相同的格式。 Visit Javascript DOMNode List for more information.访问Javascript DOMNode 列表了解更多信息。

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

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