Meteor + React:Meteor.subscribe(' Collection',limit)触发器重新呈现每个列表元素

时间:2017-04-25 19:34:40

标签: reactjs meteor publish-subscribe

我有一些带有一些设置的Meteor订阅,因此我不会发布我的整个收集服务器端。订阅将从createContainer() meteor/react-meteor-data内获取,并显示在简单的<ul>列表中,我还将document.id作为键添加到<li>元素中。

不幸的是,只要我在订阅第二个订阅参数(Meteor.subscripte('Collections', settings.limit)中增加settings.limit,整个<ul>列表就会重新呈现?如果只添加新的列表元素,我该怎么做才能增加发布限制?

P.S。当我发布总集合并通过Collection.find({}, {limit: newLimit}).fetch()更改我的客户端的限制时,react正在按预期工作:保留旧元素,只添加新元素!

客户端

import React, { Component } from 'react';
import { Locations } from '/both/collections/locations.js';
import { Events } from '/both/collections/events.js';
import { createContainer } from 'meteor/react-meteor-data';

class Content extends React.Component {
  constructor(props) {
    super(props);
    this.renderLocations = this.renderLocations.bind(this);
  }

  renderLocations() {
    return this.props.locations.map(function(location) {
      return (<li key={location._id} >{location.name}</li>);
    });
  }

  render() {
    console.log(this.props);
    return !this.props.loading && (
      <div>
    <ul>
      {this.renderLocations()}
    </ul>
      <h1 onClick={this.props.increaseLimit}> Limit </h1>
      <div style={{marginBottom: "100px"}}></div>
      </div>
    );
  }
}

export default createContainer((props) => {
  const settings = {
    limit: props.limit,
  }
  const locationsSubscribe = Meteor.subscribe('Locations', settings);
  const loading = !locationsSubscribe.ready();
  if(loading) {
    return { loading };
  } else {
    const _locations = Locations.find({}, {fields: { name: 1}, sort: { name: 1 }}).fetch();

    return {
      loading, 
      locations: _locations,
      increaseLimit: props.increaseLimit,
    };
  }
}, Content);

服务器端

Meteor.publish('Locations', function(settings) {
  return Locations.find({}, {limit: settings.limit, sort: { name: 1} } );
});

Collection.find()。fetch()响应

[
  {
    "name": "3-Master Bike-Style",
    "_id": "N9rWyZMdxEe6jhNW2"
  },
  {
    "name": "43einhalb",
    "_id": "bPgpBm59LohGLaAsf"
  },
  {
    "name": "A&B Döner",
    "_id": "qTNMk73ThvaPxGWqM"
  },
  {
    "name": "A.T.U ",
    "_id": "aWzSmp2zZ8etDhHk6"
  },
  {
    "name": "AIKO Sushibar - Hot Wok",
    "_id": "9pQJgeBNo5gFRkKdF"
  },
  {
    "name": "AXA Stefan Hahn",
    "_id": "d9f6mTrSTGCoeKPbP"
  }
]

2 个答案:

答案 0 :(得分:1)

问题在于服务器端逻辑。

您当前的代码:

Meteor.publish('Locations', function(settings) {
  return Locations.find({}, {limit: settings.limit, sort: { name: 1} } );
});

这将发送n个doc,基本上你是10,20,30等文档给客户。

修复:您需要跳过以前的文档。

<强>解决方案:

Meteor.publish('Locations', function(settings) {
  return Locations.find({}, {skip: settings.skip, limit: settings.limit, sort: { name: 1} } );
});

或者

Meteor.publish('Locations', function(settings) {
  var skip = settings.pageNumber * settings.number_of_record_per_page; //change variable according to you
  return Locations.find({}, {skip: settings.limit, limit: settings.limit, sort: { name: 1} } );
});

答案 1 :(得分:0)

好的,我终于找到了问题: 每次限制更改时,createContainer()再次订阅已发布的集合!这意味着它会向我的 Content 组件发送新道具,从而触发重新渲染!在短时间内重新订阅位置数组,它被保存为道具,将被一个空数组覆盖,这个数组只在屏幕上显示为闪光。因此,在短时间内不会显示任何内容,然后传输具有正确位置数组的新道具。

现在,只要有新位置要添加,concat()新的位置数组就会通过componentWillReceiveProps(nextProps)进入内容组件状态的解决方案。然后可以在shouldComponentUpdate(nextProps, nextState)内比较旧状态和新状态,只在状态发生变化时才更新!