【问题标题】:How to show loader in reactjs and meteor?如何在reactjs和meteor中显示加载器?
【发布时间】:2016-06-01 16:12:02
【问题描述】:

我有一个页面,其中列出了 3 个食谱以及更多按钮。单击更多按钮时,将列出更多 3 个食谱。我想做的是在列出更多 3 个食谱之前,我想显示一个微调器/加载器图标,但我只能更改按钮的文本。一旦单击更多按钮并且在列出其他 3 个食谱之前,我如何显示加载程序图标。我正在使用meteorjs和reactjs。

我的代码是

export default class Home extends Component {
    constructor(){
        super();
        this.state = { limit:3 , loading:false }
        this.addMore = this.addMore.bind(this);
    }
    getMeteorData(){
        let data = {};
        data.recipes = [];
        data.recipes = Recipes.find({},{sort:{createdAt:-1}}).fetch();
        let recipeHandle = Meteor.subscribe('recipelist',this.state.limit);
        if(recipeHandle.ready()){
            data.recipes = Recipes.find({},{sort:{createdAt:-1},limit:this.state.limit}).fetch();
        }
        return data;
    }

    addMore(){
        this.setState({
                        limit:this.state.limit + 3, loading: true }, () => {
                        this.setTimeout(()=>{
                            this.setState({loading:false});
                        },2000);
                    });
    }
    render() {
        console.log('this.data.recipes',this.data.recipes);
        let recipes = _.map(this.data.recipes,(recipe) => {
            return <RecipeList key={recipe._id} recipe={recipe} loading={this.state.loading} />
        });
        return (
            <div className="row">
                <div className="intro blink z-depth-1">
                  <div className="row">
                    <div className="col l7">
                        <h1 className="heading flow-text blink">Sell your Recipe</h1>
                    </div>
                  </div>
                </div>
                <div className="row">
                    <div className="col s12">
                        {recipes}
                    </div>
                </div>
                <button onClick={this.addMore} type="button" className="btn coral more">More</button>
            </div>
        );
    }
}
ReactMixin(Home.prototype, ReactMeteorData);

【问题讨论】:

  • getMeteorData()你可以存储加载状态:data.isLoading = !recipeHandle.ready();
  • 感谢您的知识。我得到它。我现在可以让它同时工作(一个你的方式和另一个 @omerts )。

标签: javascript node.js meteor reactjs ecmascript-6


【解决方案1】:

您可以删除加载状态,并从数据中计算加载状态:{this.state.limit !== this.data.recipes.length &amp;&amp; &lt;img src="path_to_loader.gif" /&gt;}

我不确定你想在哪里显示加载器,但例如你可以这样做:

render() {
  console.log('this.data.recipes',this.data.recipes);
  let recipes = _.map(this.data.recipes,(recipe) => {
      return <RecipeList key={recipe._id} recipe={recipe} loading={this.state.loading} />
  });
  return (
      <div className="row">
          <div className="intro blink z-depth-1">
            <div className="row">
              <div className="col l7">
                  <h1 className="heading flow-text blink">Sell your Recipe</h1>
              </div>
            </div>
          </div>
          <div className="row">
              <div className="col s12">
                  {recipes}
              </div>
          </div>
          {this.state.limit !== this.data.recipes.length && <img src="path_to_loader.gif" />}
          <button onClick={this.addMore} type="button" className="btn coral more">More</button>
      </div>
  );
}

【讨论】:

  • 我应该在哪里提供 setTimeout 以便我可以再加载几秒钟的状态?
  • 我认为您不需要 setTimeout,第一次渲染将发生在 setState 上,您将状态更改为正在加载。在 Meteor 加载更多数据后,另一个渲染应该会自动发生(根据我对 Meteor 的理解)。
猜你喜欢
  • 1970-01-01
  • 2013-07-21
  • 1970-01-01
  • 2019-10-28
  • 1970-01-01
  • 1970-01-01
  • 2016-05-24
  • 2015-11-20
  • 1970-01-01
相关资源
最近更新 更多