反应原生动态参考

时间:2018-05-22 22:44:03

标签: javascript android reactjs react-native

我正在尝试从specefic视图中获取一些测量值,以查看视图的位置。 之后我想实现一个scrollto函数。

我正在使用此代码来处理句柄:

_handleItemClicked = (index) => () => {
  this.sectionItem[index].measure( (fx, fy, width, height, px, py) => {
    console.log('Component width is: ' + width)
    console.log('Component height is: ' + height)
    console.log('X offset to frame: ' + fx)
    console.log('Y offset to frame: ' + fy)
    console.log('X offset to page: ' + px)
    console.log('Y offset to page: ' + py)
  })
}

我正在使用此代码进行视图

renderItem = ({item, index, section}) => (
            <TouchableNativeFeedback ref={view => { this.sectionItem[index] = view; }} onPress={this._handleItemClicked(index)}>
               <View style={styles.itemrow} >
                 <View style={{width:80,marginLeft:10,height:30}}><Text style={{textAlign:'right',color:'#000',opacity:0.9,fontSize:18,marginTop:4}}>{item.title}</Text></View>
                 </View>
               </View>
             </TouchableNativeFeedback>
);

我尝试了其他一些方法,但到目前为止还没有运气。 我总是以一些错误结束。这次它的未定义不是一个对象。

期待着答案。

1 个答案:

答案 0 :(得分:0)

尝试以下方法:

ref = { view => ( this[`sectionItem${index}`] = view ) }

我猜想它试图在范围内找到sectionItem,但是无法找到未定义的错误。

如果您像上述那样操作,则会在其中放置带有索引的ref。

相关问题