简体   繁体   English

ReactJS-在Prop中使用嵌套数组的Rending Map函数

[英]ReactJS - Rending Map Function with Nested Array in Prop

I'm trying to figure out how to map a nested array in my prop and also determine if there is a better, best practice, way to split out this logic into a separate component. 我试图弄清楚如何在我的道具中map一个嵌套数组,并确定是否有更好的最佳实践将这种逻辑拆分为一个单独的组件。 I thought I had followed the correct approach below, but receive an error at my opening { for the start of the nested map method. 我以为自己遵循了下面的正确方法,但是在开始时{嵌套map方法的开始出现错误。

Error message: 错误信息:

ERROR in ./public/components/app/comment-box/comment.jsx
Module build failed: SyntaxError: Unexpected token, expected , (46:12)

  44 |         <div>
  45 |         { props.records.map((record, index) => <RecordCard {...record} />
> 46 |             {
     |             ^
  47 |                 { record.record_comments.map((comment, i) => 

Code: 码:

  //GET /api/test and set to state
    class RecordsFeedContainer extends React.Component{
        constructor(props, context) {
            super(props, context);
            this.state = this.context.data || window.__INITIAL_STATE__ || {records: []};
        }

        fetchList() {
            fetch('http://localhost:3000/api/test')
                .then(res => {
                    return res.json();
                })  
                .then(data => {
                    this.setState({ records: data });
                })
                .bind(this)  
                .catch(err => {
                    console.log(err);
                });
        }

        componentDidMount() {
            this.fetchList();
        }

        render() {
            return (
                <div>
                <h2>Records List</h2>
                <ul>
                <RecordsFeed {...this.state} />
                </ul>
                </div>
            )
        }
    };

    //Loop through JSON and component
    const RecordsFeed = props => {
            return (
            <div>
            { props.records.map((record, index) => <RecordCard {...record} />
                {
                    { record.record_comments.map((comment, i) => <Comments {...comment} />)}
                }
            )}
            </div>
            )
    }

JSX props.records.map((record, index) => <RecordCard {...record} /> JSX props.records.map((record, index) => <RecordCard {...record} />

is turned into 被变成

props.records.map((record, index) => {
    return React.createElement(RecordCard, ...record);
}

Your code would be 您的代码将是

props.records.map((record, index) => {
    return React.createElement(RecordCard, ...record) { { record.record_comments.map...;
}

I'm guessing comments should be inside the cards, right? 我猜评论应该在卡片里面,对不对? So then you could do this instead: 因此,您可以改为执行以下操作:

{ props.records.map((record, index) => {
    return (
        <RecordCard {...record}>
            { record.record_comments.map((comment, i) => <Comments {...comment} /> )}
        </RecordCard>
    );
} )}

Then inside RecordCard you would add {this.props.children} somewhere in it's render method's returned JSX. 然后在RecordCard ,将{this.props.children}添加到其render方法的返回JSX中。

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

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