2016-11-19 55 views
0

我希望能夠獲得我分配的所有參考文獻的measure()。這就是我想要管理的內容:是否可以將參數放入數組中?

<View 
    ref="dropZone[]"> 
</View> 
<View 
    ref="dropZone[]"> 
</View> 

lets dropZones = [] 
for(let i=0; i < refs.length; i++){ 
    this.refs[i].measure((ox, oy, width, height, px, py) => { 
     dropZones.push({x: px, y:py, height: height, width: width}) 
    } 
} 

什麼是最好的管理方式?

+0

是否行得通?如果它有效 - 它是「最好的」,否則它不是。 – zerkms

+0

我只是試圖用這種方式展示一個例子。甚至可以在道具中聲明一個數組? –

+1

這是可能的 - https://facebook.github.io/react/docs/refs-and-the-dom.html – zerkms

回答

0

使用箭頭功能我設法做這樣的render()

<View 
    ref={c => this.dropZones.set(0, c)}> 
</View> 
<View 
    ref={c => this.dropZones.set(1, c)}> 
</View> 

而且我用這個在componentDidMount()

Array.from(this.dropZones.values()) 
    .forEach(dropZone => { 
    dropZone.measure((ox, oy, width, height, px, py) => { 
     dropZonesArray.push({x: px, y:py, height: height, width: width}) 
    }) 
}) 
相關問題