在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