javascript - 从 React 中的渲染组件中提取元素数组
问题描述
我正在使用React
和 pnp 控件为 SharePoint Online 开发 spfx Web 部件
我正在使用 pnp 控制轮播
<Carousel
buttonsLocation={CarouselButtonsLocation.top}
buttonsDisplay={CarouselButtonsDisplay.block}
contentContainerStyles={styles.carouselContent}
containerButtonsStyles={styles.carouselButtonsContainer}
isInfinite={true}
element={this.carouselElements}
onMoveNextClicked={(index: number) => { console.log(`Next button clicked: ${index}`); }}
onMovePrevClicked={(index: number) => { console.log(`Prev button clicked: ${index}`); }}
/>
在上面的控制元素={this.carouselElements}
属性需要基于“键”属性区分的元素数组
现在我创建了另一个组件,呈现如下:
public render(): React.ReactElement<IHomePageCarouselProps> {
return (
<div className={styles.homePageCarousel}>
{/* <div className="owl-carousel owl-theme owl-banner "> */}
<div key="1">
<a href="#">
<img src="images/banner_1.jpg " alt="banner" className="rounded-top" />
</a>
<div className="bg-white rounded-bottom p-10">
<span className="font-color-green font-weight-bold ">
News title will show here. News title will show here. News title will show
here. News title will show here. News title will show here. News title will
show here. News title will show here. News title will show here. ...
</span>
</div>
</div>
<div key="2">
<a href="#">
<img src="images/banner_1.jpg " alt="banner" className="rounded-top" />
</a>
<div className="bg-white rounded-bottom p-10">
<span className="font-color-green font-weight-bold ">
News title will show here. News title will show here. News title will show
here. News title will show here. News title will show here. News title will
show here. News title will show here. News title will show here. ...
</span>
</div>
</div>
{/* </div> */}
</div>
)
}
在 Carousel 元素属性中使用上述组件,如下所示:
element={<HomePageCarousel />}
但HomePageCarousel
不能在没有 main 的情况下渲染,div
这是React
. 有人可以帮助我如何输出或提取元素数组吗?
解决方案
要在 React 中呈现元素列表,您可以使用 React.Fragment
return(
<React.Fragment>
<div key="1">
<a href="#">
<img src="images/banner_1.jpg " alt="banner" className="rounded-top" />
</a>
<div className="bg-white rounded-bottom p-10">
<span className="font-color-green font-weight-bold ">
News title will show here. News title will show here. News title will show
here. News title will show here. News title will show here. News title will
show here. News title will show here. News title will show here. ...
</span>
</div>
</div>
<div key="2">
<a href="#">
<img src="images/banner_1.jpg " alt="banner" className="rounded-top" />
</a>
<div className="bg-white rounded-bottom p-10">
<span className="font-color-green font-weight-bold ">
News title will show here. News title will show here. News title will show
here. News title will show here. News title will show here. News title will
show here. News title will show here. News title will show here. ...
</span>
</div>
</div>
</React.Fragment>
)
推荐阅读
- javascript - 复杂的 javascript 代码行导致 Internet Explorer 11 中的错误
- regex - Apache htaccess 重定向到 https 所有 url,除了某些带有各种参数的 URL
- sql-server - 使用 Union All 和 Group by ID 和 Description 组合 4 个表
- react-native - 在 react-native 中结合 Stack、Drawer 和 Tab 导航器
- mysql - MySQL如何检查整数是否包含0?
- flutter - 如何将 CustomScrollView 用作孩子
- python - 如何将循环打印输出更改为熊猫数据框
- django - 如何将值保存在 django 的数据库中?
- python - 从嵌套字典中的嵌套字典创建 Pandas 数据框
- java - 对 Spring Controller 的请求返回 404 not found