我也ReduxLazyScroll
用于无限滚动效果,它有效,但在它只是ul -> li
列表之前。现在我希望我的应用程序以引导网格样式并排生成 3 张卡片,如下所示:
<Row>
<Col sm="4">.col-3</Col>
<Col sm="4">.col-3</Col>
<Col sm="4">.col-3</Col>
</Row>
而且我有一些逻辑问题,我无法解决。于是就有了父组件(BeerListingScroll
)的代码:
import React, { Component } from 'react';
import { Container, Row, Col } from 'reactstrap';
import ReduxLazyScroll from 'redux-lazy-scroll';
import { connect } from 'react-redux';
import { bindActionCreators } from 'redux';
import { fetchBeers } from '../../actions/';
import BeersListItem from '../../components/BeersListItem';
import ProgressIndicator from '../../components/ProgressIndicator';
class BeerListingScroll extends Component {
constructor(props) {
super(props);
this.loadBeers = this.loadBeers.bind(this);
}
loadBeers() {
const { skip, limit } = this.props.beers;
this.props.fetchBeers(skip, limit);
}
render() {
const { beersArray, isFetching, errorMessage, hasMore } = this.props.beers;
return (
<Container>
<ReduxLazyScroll
isFetching={isFetching}
errorMessage={errorMessage}
loadMore={this.loadBeers}
hasMore={hasMore}
>
{beersArray.map(beer => (
<Row>
<Col xs="12" sm="4">
<BeersListItem key={beer.id} beer={beer} />
</Col>
<Col xs="12" sm="4">
<BeersListItem key={beer.id} beer={beer} />
</Col>
<Col xs="12" sm="4">
<BeersListItem key={beer.id} beer={beer} />
</Col>
</Row>
))}
</ReduxLazyScroll>
</Container>
);
}
}
function mapStateToProps(state) {
return {
beers: state.beers,
};
}
function mapDispatchToProps(dispatch) {
return bindActionCreators({ fetchBeers }, dispatch);
}
export default connect(mapStateToProps, mapDispatchToProps)(BeerListingScroll);
所以我使用 beersArray.map 助手,我知道这个逻辑在里面没有意义map
:
<Row>
<Col xs="12" sm="4">
<BeersListItem key={beer.id} beer={beer} />
</Col>
<Col xs="12" sm="4">
<BeersListItem key={beer.id} beer={beer} />
</Col>
<Col xs="12" sm="4">
<BeersListItem key={beer.id} beer={beer} />
</Col>
</Row>
因为我将每个beer
元素加载了 3 次,然后看起来像这样 -> img
问题是:我应该如何重构该代码以获取第一行中的第一个、第二个和第三个数组元素,第二行中的第 4 个、第 5 个、第 6 个等?
期望的结果:
这里有子组件 ( BeerListItem
)
import React from 'react';
import {
Card,
CardImg,
CardBody,
CardTitle,
CardSubtitle,
Button,
} from 'reactstrap';
const BeerListItem = ({ beer }) => (
<div>
<Card>
<CardImg top width="100%" src={beer.image_url} alt="beer" />
<CardBody>
<CardTitle>{beer.name}</CardTitle>
<CardSubtitle>{beer.tagline}</CardSubtitle>
<Button>More details</Button>
</CardBody>
</Card>
</div>
);
export default BeerListItem;
以及 github 上的完整项目 - >链接到 github