我在使用方法的React組件中使用了array.map
。它的工作原理,但我想知道它渲染了多少行。我想通過我的構造函數初始化this.numRows = 0
,然後在map
回調遞增它:獲取地圖在React渲染中返回的數組大小
<div>
<p>Number of rows = {this.numRows}</p>
{this.state.members.map((member) => {
if (member.display) {
this.numRows++;
return <p>{ member.name }</p>
}
})}
</div>
但這仍顯示爲零。完整的代碼在這裏:jsfiddle.net/ra13jxak/。我怎樣才能顯示返回的行數?
您是否有會員不會在您的「會員」狀態中呈現? –
在你的構造函數中,用'this.state = {numRows:0}'初始化一個狀態,然後在if語句內執行:this.setState({numRows:this.state.numRows + 1})'
行數= {this.state.numRows}
' – finch