2016-05-14 125 views
0

有人可以幫我用下面的代碼嗎?組件工作正常,但它不是渲染列表項,我無法找到一個方法來返回渲染功能列表中的項目react渲染列表項

import React from 'react' 
import PageLinks from './PageLinks' 

const PaginationSection = React.createClass({ 

onClickHandle (pageNumber) { 
this.props.pageNumberClicked(pageNumber) 
}, 

    renderPageNumbers() { 
    let page = 1; 
    console.log('inside render'); 
    while (page < this.props.totalPages) { 
     console.log(this.props.totalPages); 
     console.log(page); 
     if (page == this.props.currentPage) { 
     <li key={page}><span>&nbsp;</span></li> 
     } 
     else { 
     <li key={page}><PageLinks pageNumber={page} onPageClick={this.onClickHandle} /></li> 
     } 
     page++; 
    } 
    }, 

    render() { 
    return (
     <div> 
     { 
      this.props.totalPages > 1 
      ? <ul className="pagination"> 
       {this.renderPageNumbers()} 
      </ul> 
     : <div>&nbsp;</div> 
     } 
     </div> 
)} 
}) 

export default PaginationSection 

回答

0

你需要從renderPageNumbers方法返回元素的數組,例如:

renderPageNumbers() { 
    let page = 1; 
    const pages = []; 
    while (page < this.props.totalPages) { 
    if (page == this.props.currentPage) { 
     pages.push(<li key={page}><span>&nbsp;</span></li>); 
    } 
    else { 
     pages.push(<li key={page}><PageLinks pageNumber={page} onPageClick={this.onClickHandle} /></li>); 
    } 
    page++; 
    } 
    return pages; 
}, 
+0

非常感謝@ madox2。有效。你是一個救星.. :) :) – Mah3ndra

+0

@ Mah3ndra歡迎您:-) – madox2