简体   繁体   English

如何在循环内打印功能组件-React JS

[英]How to print a functional component inside loop - react js

I want to print my functional component inside a loop. 我想在循环中打印我的功能组件。 but I got this error: 但是我得到了这个错误:

SyntaxError: Unexpected token, expected

my code: 我的代码:

render(){
 ...

return (

...


    { this.state.coupons.length > 0 ? (

                                    Object.keys(reducedData).map(obj => {

                                              var row = {} ;
                                              row.title = '';
                                              row.coupon_code = '';
                                                reducedData[obj].map(item =>
                                                    {
                                                      row.title = item.title;
                                                      row.coupon_code += item.coupon_code;
                                                    }

                                                     {<MyShadyabsRow  row={row}/>}


                                              )
                                        })

                                  ) : (
                                    <tr></tr>
                                  )

                                  }

...

)

}

my error line is at this line: 我的错误行是在这一行:

{<MyShadyabsRow  row={row}/>}

my functional component: 我的功能组件:

import React from ‘react’;

const MyShadyabsRow = ({row}) => (
  <tr>
      <td>
          <a href="#">
              <i className="fa fa-print" aria-hidden="true">
                  <span>پرینت</span>
              </i>
          </a>
      </td>
      <td>{row.title}</td>
      <td>۱۳۹۶/۱۰/۱۰</td>
      <td>{row.coupon_code}</td>
  </tr>
);

export default MyShadyabsRow;

actually what are missing is the return statement inside the render and also the different closures of the functions are not correct, for example: 实际上缺少的是渲染器内部的return语句,并且函数的不同闭包都不正确,例如:

                                            reducedData[obj].map(item =>
                                                {
                                                  row.title = item.title;
                                                  row.coupon_code += item.coupon_code;
                                                }

                                                 {<MyShadyabsRow  row={row}/>}
                                          )

as you may see, you are closing the `(item => {....} ) which is not corret. 如您所见,您正在关闭不是corret的`(item => {....})。

this.state.coupons.length > 0 ? (
        Object.keys(reducedData).map(obj => {
            let row = {};
            row.title = '';
            row.coupon_code = '';
            reducedData[obj].map(item => {
                row.title = item.title;
                row.coupon_code += item.coupon_code;
            })
            //this will return the object after the map is applied.
            //the error was on the closure of your code and the use of {} wrapping your component.
            return (<MyShadyabsRow  row = { row } />)
        })): (
            <tr></tr>
        )

The line in question is indeed wrong. 所讨论的行确实是错误的。 You need to return the component, and right now you're just wrapping it in curly brackets, which makes no sense in that context. 您需要返回该组件,现在您只是将其包装在大括号中,在这种情况下这没有任何意义。

That faulty line should instead be 那条错误的线应该是

return <MyShadyabsRow  row={row}/>;

I'm assuming you want to render one MyShadyabsRow component for each key in reducedData . 我假设你要呈现一个MyShadyabsRow组件在每个关键reducedData

Map function will return multiple components and since you have outer object key it will iterate again. Map函数将返回多个组件,并且由于您具有外部对象键,因此它将再次迭代。 So from map function you cannot render JSX component you map compenent to each element of array. 因此,无法通过map函数来渲染将组件映射到数组每个元素的JSX组件。 that is why bracket syntax is not valid. 这就是括号语法无效的原因。

{ this.state.coupons.length > 0 ? (
   Object.keys(reducedData).map(obj => {
       var row = {} ;
       row.title = '';
       row.coupon_code = '';
       return reducedData[obj].map(item =>
                                   {
                                      row.title = item.title;
                                      row.coupon_code += item.coupon_code;
                                      return (<MyShadyabsRow  row={row}/>)
                                    }





                                   )
        })

Your {<MyShadyabsRow row={row}/>} is inside the map function 您的{<MyShadyabsRow row={row}/>}位于map函数内部

reducedData[obj].map(item =>{
      row.title = item.title;
      row.coupon_code += item.coupon_code;
      return (<MyShadyabsRow  row={row}/>);
) 

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

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