我有一个使用 array.map
呈现组件的 javascript 数组。我将此数组切换为 es6 Map
以便能够使用键值对更轻松地查找项目,并从 .map
切换为 —5cafa6f60fcd76b2e2d8e61ba forEach
在地图上。在 forEach
中,我调用了一个返回 React 组件的渲染方法,但它没有被渲染。如何在 forEach
中渲染组件?
<div className='gallery__items'>
{resultsByGuid.forEach((result, index) => {
key++;
this.renderGalleryItem(result, key);
})}
</div>
这是 renderGalleryItem 方法:
renderGalleryItem = (item, index) => {
const { gridItemSelected, itemThumbnailRequested } = this.props;
return (
<GalleryItem
key={index}
item={item}
onClick={gridItemSelected}
fetchThumbnailFunc={itemThumbnailRequested}
/>
);
};
我知道 forEach 不会返回任何东西,但这是否意味着我无法在其中渲染?
原文由 alsoALion 发布,翻译遵循 CC BY-SA 4.0 许可协议
只是对 danday74 使用数组解构的示例稍作改进。带有选项的 ES6 地图: