4

我正在使用反应和流星,我无法一次从服务器获取数据。组件正在流中获取数据,并且组件的渲染函数被多次调用。

这是我用来从带有 mixin 的 react 组件内的客户端上的服务器获取帖子的代码。

getMeteorData() {
        return {
            posts: Posts.find({}, {sort: {createdAt: -1}}).fetch()
        }
    },

到目前为止,这是从服务器获取所有帖子(那里只有大约 20 个)

如何一次从服务器获取数据,使其不会多次流式传输和调用渲染函数?

4

1 回答 1

2

Add extra check if the loading of the data is done for example:

  mixins: [ ReactMeteorData ],
  getMeteorData() {
    var subscription = Meteor.subscribe( 'posts' );

    return {
      isLoading: !subscription.ready(),
      posts: Posts.find({}, {sort: {createdAt: -1}}).fetch()
    };
  },
  render() {
    if ( this.data.isLoading ) {
      return <div>Loading...</div>;
    } else {
      return (
        // now we have data and render once
      );
    }
  }
于 2015-12-16T08:11:53.233 回答