首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何基于数组数据React渲染多个实例

如何基于数组数据React渲染多个实例
EN

Stack Overflow用户
提问于 2021-02-18 19:24:30
回答 3查看 40关注 0票数 0

我有一个数组,其中的对象包含不同的坐标,例如:

代码语言:javascript
复制
bottomRow: 130.8201356
leftCol: 52.33386899
rightCol: 279.71340993
topRow: 48.9834268

我需要的是渲染基于此数据的正方形。数组中有多少不同的对象,就有多少个正方形。

我尝试过这段代码,但没有成功:

代码语言:javascript
复制
const FaceRecognition = ({imageUrl, box}) => {
     console.log('recog',box)
  return (
    <div className='item-e center ma'>
        <div className='absolute mt2 z-2'>
            <img className='image br4 ma4 z-2' id='inputImage' src={imageUrl} alt=''/>
                {   box.forEach(() => {
                    return (
                        <div className='bounding-box' style={{top: box.topRow, right: box.rightCol, bottom: box.bottomRow, left: box.leftCol}}>
                        </div>
                    )
            }
                
                )
            }
        </div>
    </div>
  );

}

我应该如何解决这个问题,这样它才能获得所有的对象并渲染所有的方块?到目前为止,我不得不将或或任何其他数组索引放在box旁边,它只呈现那个对象。但我需要一次性渲染所有内容。

更新:

我尝试过box.map,如下所示:

代码语言:javascript
复制
const FaceRecognition = ({imageUrl, box}) => {
     console.log('recog',box)
  return (
    <div className='item-e center ma'>
        <div className='absolute mt2 z-2'>
            <img className='image br4 ma4 z-2' id='inputImage' src={imageUrl} alt=''/>
                { box.map(((topRow, rightCol, leftCol, bottomRow), idx) => {
                        <div key={idx} className='bounding-box' style={{top: box.topRow, right: box.rightCol, bottom: box.bottomRow, left: box.leftCol}}>
                        </div>
                        }
                    )
                }
        </div>
    </div>
  );
}

仍然没有成功,我现在得到了Invalid parenthesized assignment pattern,但不知道为什么

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2021-02-18 19:55:25

首先,您应该使用映射,因为forEach将返回undefined。其次,你忘了在map/forEach中使用控制变量,你的map应该这样做:

代码语言:javascript
复制
const FaceRecognition = ({imageUrl, box}) => {
     console.log('recog',box)
  return (
    <div className='item-e center ma'>
        <div className='absolute mt2 z-2'>
            <img className='image br4 ma4 z-2' id='inputImage' src={imageUrl} alt=''/>
                {   box.map((element) => {
                    return (
                        <div className='bounding-box' style={{top: element.topRow, right: element.rightCol, bottom: element.bottomRow, left: element.leftCol}}>
                        </div>
                    )
            }
                
                )
            }
        </div>
    </div>
  );

}

你在那里使用的方框是完整的数组,所以不可能得到这个元素。

票数 2
EN

Stack Overflow用户

发布于 2021-02-18 19:53:59

您应该尝试map函数,并在map函数中添加一个arg。

代码语言:javascript
复制
const FaceRecognition = ({ imageUrl, box }) => {
  return (
    <div className='item-e center ma'>
      <div className='absolute mt2 z-2'>
        <img className='image br4 ma4 z-2' id='inputImage' src={imageUrl} alt='' />
        {box.map((item) => {
          return (
            <div className='bounding-box' style={{ top: item.topRow, right: item.rightCol, bottom: item.bottomRow, left: item.leftCol }}>
            </div>
          )
        })
        }
      </div>
    </div>
  );
}
票数 1
EN

Stack Overflow用户

发布于 2021-02-18 20:09:54

代码语言:javascript
复制
const FaceRecognition = ({imageUrl, box}) => {
    console.log('recog',box)
    return (
        <div className='item-e center ma'>
            <div className='absolute mt2 z-2'>
                <img className='image br4 ma4 z-2' id='inputImage' src={imageUrl} alt=''/>
                {box.map(({topRow, rightCol, leftCol, bottomRow}, idx) => (
                    <div
                       key={idx}
                       className='bounding-box'
                       style={{
                           top: topRow, 
                           right: rightCol, 
                           bottom: bottomRow, 
                           left: leftCol
                       }}>
                    </div>
                ))}
            </div>
        </div>
   );
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66258990

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档