2016-06-01 15 views
1

我有一個頁面,其中3個食譜與更多按鈕一起列出。點擊更多按鈕時,會列出更多3個配方。我想要做的是在列出更多3個食譜之前,我想顯示一個微調器/加載器圖標,但我只能改變按鈕的文本。只要點擊更多按鈕,並且在列出其他3個配方之前,我如何顯示加載器圖標。我正在使用meteorjs和reactjs。如何在reactjs和meteor中顯示加載程序?

我對這個代碼是

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); 
+1

在'getMeteorData()'你可以存儲加載狀態:'data.isLoading =!recip eHandle.ready();' – aedm

+0

感謝您的寶貴意見。我知道了。我現在可以雙向工作(你的方式和另一個@omerts)。 – milan

回答

1

您可以刪除加載狀態,只是計算從數據加載狀態:{this.state.limit !== this.data.recipes.length && <img src="path_to_loader.gif" />}

我不知道,你要顯示的例如你可以這樣做:

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> 
); 
} 
+0

我應該在哪裏提供setTimeout,這樣我可以加載狀態更多秒? – milan

+0

我不認爲你需要setTimeout,第一次渲染將發生在setState上,在這裏你將狀態改變爲加載。在流星加載更多數據後,另一個渲染應該自動發生(根據我對Meteor的理解)。 – omerts

相關問題