在Meteor 1.4中使用React我想做一个渲染组件的嵌套循环,每行6个项目的2行。通过流星React组件循环
Row 1
[unique_item] [unique_item] [unique_item] ...
Row 2
[unique_item] [unique_item] [unique_item] ...
我如何通过状态到createContainer
功能,这样我可以增加计数器进行分页的结果?
下面的代码:
import React, { Component, PropTypes } from 'react';
import ReactDOM from 'react-dom';
import { createContainer } from 'meteor/react-meteor-data';
import { Items } from '../api/items.js';
import Item from './Item.jsx';
import '../../client/stylesheets/main.scss';
class App extends Component {
constructor(props) {
super(props);
this.state = {
skipCount : 0
};
}
renderItemRows(i) {
return (
<div className="container-fluid">
<div className="row">
{ this.renderItems(i) }
</div>
</div>
);
}
renderItems(i) {
// i here has the right value...how do I pass into createContainer?
return this.props.items.map((item) => (
<Item key={item._id} item={item} />
));
}
render() {
let rows = [];
for (let i=0;i<2;i++) {
rows.push(this.renderItemRows(i));
}
return (<div>{rows}</div>);
}
}
App.propTypes = {
items: PropTypes.array.isRequired,
skipCount: PropTypes.number,
};
export default createContainer(() => {
// Hardcoded to 50 just to make sure the data pagination works
const skipCount = 50;
Meteor.subscribe('items', skipCount);
return {
items: Items.find({}, { sort: { item : 1 }, limit : 6 }).fetch(),
};
}, App);
通过this thread对流星论坛看完后,createContainer()
作为一个无状态的函数传递所以没有办法像这样的信息传递给它。
根据这一线索,我只有两个选项:
- 通过
Session.get()
创建一个包装组件(ItemRow1.jsx
和ItemRow2.jsx
)
第1号将工作,但似乎令人难以置信的重复只是通过第二行数据分页......有没有更好的方法来做到这一点?
我不确定我是否理解你想达到的目标。你的问题与你的'i'变量或者'skipCount'有关吗(在你的代码中你可能会遇到问题)?你想动态渲染可变数量的行,还是一直是这两个? – Waiski
我想总是呈现每行6个项目的2行。 'skipCount'只是对结果进行分页,因此第1行是1-6,第2行是7-12。 – OrdinaryHuman